C# 将 XDocument 转换为 XmlDocument,反之亦然
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1508572/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Converting XDocument to XmlDocument and vice versa
提问by Wim ten Brink
It's a very simple problem that I have. I use XDocument to generate an XML file. I then want to return it as a XmlDocument class. And I have an XmlDocument variable which I need to convert back to XDocument to append more nodes.
这是我遇到的一个非常简单的问题。我使用 XDocument 生成一个 XML 文件。然后我想将它作为 XmlDocument 类返回。我有一个 XmlDocument 变量,我需要将其转换回 XDocument 以附加更多节点。
So, what is the most efficientmethod to convert XML between XDocument and XmlDocument? (Without using any temporary storage in a file.)
那么,在 XDocument 和 XmlDocument 之间转换 XML的最有效方法是什么?(不使用文件中的任何临时存储。)
采纳答案by Mark Coleman
You can use the built in xDocument.CreateReader() and an XmlNodeReader to convert back and forth.
您可以使用内置的 xDocument.CreateReader() 和 XmlNodeReader 来回转换。
Putting that into an Extension method to make it easier to work with.
将其放入 Extension 方法中以使其更易于使用。
using System;
using System.Xml;
using System.Xml.Linq;
namespace MyTest
{
internal class Program
{
private static void Main(string[] args)
{
var xmlDocument = new XmlDocument();
xmlDocument.LoadXml("<Root><Child>Test</Child></Root>");
var xDocument = xmlDocument.ToXDocument();
var newXmlDocument = xDocument.ToXmlDocument();
Console.ReadLine();
}
}
public static class DocumentExtensions
{
public static XmlDocument ToXmlDocument(this XDocument xDocument)
{
var xmlDocument = new XmlDocument();
using(var xmlReader = xDocument.CreateReader())
{
xmlDocument.Load(xmlReader);
}
return xmlDocument;
}
public static XDocument ToXDocument(this XmlDocument xmlDocument)
{
using (var nodeReader = new XmlNodeReader(xmlDocument))
{
nodeReader.MoveToContent();
return XDocument.Load(nodeReader);
}
}
}
}
Sources:
资料来源:
回答by Daren Thomas
You could try writing the XDocument to an XmlWriter piped to an XmlReader for an XmlDocument.
您可以尝试将 XDocument 写入通过管道传输到 XmlDocument 的 XmlReader 的 XmlWriter。
If I understand the concepts properly, a direct conversion is not possible (the internal structure is different / simplified with XDocument). But then, I might be wrong...
如果我正确理解这些概念,则无法直接转换(内部结构与 XDocument 不同/简化)。但是,我可能错了......
回答by paul
There is a discussion on http://blogs.msdn.com/marcelolr/archive/2009/03/13/fast-way-to-convert-xmldocument-into-xdocument.aspx
http://blogs.msdn.com/marcelolr/archive/2009/03/13/fast-way-to-convert-xmldocument-into-xdocument.aspx上有讨论
It seems that reading an XDocument via an XmlNodeReaderis the fastest method. See the blog for more details.
通过XmlNodeReader读取 XDocument 似乎是最快的方法。有关更多详细信息,请参阅博客。
回答by Robert Harvey
using System.Xml;
using System.Xml.Linq;
#region Extention Method
public static XElement ToXElement(this XmlElement element)
{
return XElement.Parse(element.OuterXml);
}
public static XmlElement ToXmlElement(this XElement element)
{
var doc = new XmlDocument();
doc.LoadXml(element.ToString());
return doc.DocumentElement;
}
#endregion
Usage of this Extention are than done simply using something like this
这个扩展的使用不仅仅是简单地使用这样的东西
System.Xml.XmlElement systemXml = (new XElement("nothing")).ToXmlElement();
System.Xml.Linq.XElement linqXml = systemXml.ToXElement();
回答by Dmitry Pavlov
If you need to convert the instance of System.Xml.Linq.XDocument into the instance of the System.Xml.XmlDocument this extension method will help you to do not lose the XML declarationin the resulting XmlDocument instance:
如果您需要将 System.Xml.Linq.XDocument 的实例转换为 System.Xml.XmlDocument 的实例,此扩展方法将帮助您不丢失生成的 XmlDocument 实例中的 XML 声明:
using System.Xml;
using System.Xml.Linq;
namespace www.dimaka.com
{
internal static class LinqHelper
{
public static XmlDocument ToXmlDocument(this XDocument xDocument)
{
var xmlDocument = new XmlDocument();
using (var reader = xDocument.CreateReader())
{
xmlDocument.Load(reader);
}
var xDeclaration = xDocument.Declaration;
if (xDeclaration != null)
{
var xmlDeclaration = xmlDocument.CreateXmlDeclaration(
xDeclaration.Version,
xDeclaration.Encoding,
xDeclaration.Standalone);
xmlDocument.InsertBefore(xmlDeclaration, xmlDocument.FirstChild);
}
return xmlDocument;
}
}
}
Hope that helps!
希望有帮助!
回答by Abhi
For me this single line solution works very well
对我来说,这个单行解决方案效果很好
XDocument y = XDocument.Parse(pXmldoc.OuterXml); // where pXmldoc is of type XMLDocument
回答by bc3tech
If you need a Win 10 UWP compatible variant:
如果您需要兼容 Win 10 UWP 的变体:
using DomXmlDocument = Windows.Data.Xml.Dom.XmlDocument;
public static class DocumentExtensions
{
public static XmlDocument ToXmlDocument(this XDocument xDocument)
{
var xmlDocument = new XmlDocument();
using (var xmlReader = xDocument.CreateReader())
{
xmlDocument.Load(xmlReader);
}
return xmlDocument;
}
public static DomXmlDocument ToDomXmlDocument(this XDocument xDocument)
{
var xmlDocument = new DomXmlDocument();
using (var xmlReader = xDocument.CreateReader())
{
xmlDocument.LoadXml(xmlReader.ReadOuterXml());
}
return xmlDocument;
}
public static XDocument ToXDocument(this XmlDocument xmlDocument)
{
using (var memStream = new MemoryStream())
{
using (var w = XmlWriter.Create(memStream))
{
xmlDocument.WriteContentTo(w);
}
memStream.Seek(0, SeekOrigin.Begin);
using (var r = XmlReader.Create(memStream))
{
return XDocument.Load(r);
}
}
}
public static XDocument ToXDocument(this DomXmlDocument xmlDocument)
{
using (var memStream = new MemoryStream())
{
using (var w = XmlWriter.Create(memStream))
{
w.WriteRaw(xmlDocument.GetXml());
}
memStream.Seek(0, SeekOrigin.Begin);
using (var r = XmlReader.Create(memStream))
{
return XDocument.Load(r);
}
}
}
}