Answers:
您可以使用内置的xDocument.CreateReader()和XmlNodeReader来回转换。
将其放入扩展方法中可以使其更容易使用。
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);
}
}
}
}
资料来源:
XmlDocument
(作为属性)和XDocument
(作为节点)之间对声明的处理不同。如果要保留声明,则需要显式处理它(请参阅blogs.msdn.com/b/ericwhite/archive/2010/03/05/…或@Dmitry的答案stackoverflow.com/a/8894680/2688)
对我来说,单线解决方案效果很好
XDocument y = XDocument.Parse(pXmldoc.OuterXml); // where pXmldoc is of type XMLDocument
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
而不是简单地使用这种扩展来使用此扩展
System.Xml.XmlElement systemXml = (new XElement("nothing")).ToXmlElement();
System.Xml.Linq.XElement linqXml = systemXml.ToXElement();
如果您需要将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;
}
}
}
希望有帮助!
您可以尝试将XDocument写入通过XmlReader传递给XmlDocument的XmlWriter。
如果我正确理解这些概念,则无法进行直接转换(内部结构与XDocument不同/已简化)。但是那我可能是错的...
关于http://blogs.msdn.com/marcelolr/archive/2009/03/13/fast-way-to-convert-xmldocument-into-xdocument.aspx的讨论
似乎通过XmlNodeReader读取XDocument 是最快的方法。请参阅博客以获取更多详细信息。
如果您需要兼容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);
}
}
}
}