扩展XmlDocument
public static class XmlDocumentExtensions
{
public static XDocument ToXDocument( this XmlDocument document)
{
return document.ToXDocument( LoadOptions .None);
}
public static XDocument ToXDocument( this XmlDocument document, LoadOptions options)
{
using ( XmlNodeReader reader = new XmlNodeReader (document))
{
return XDocument .Load(reader, options);
}
}
}
使用例子
XmlDocument doc = new XmlDocument ();
doc.LoadXml( "<parent><child>text</child></parent>" );
XDocument xdoc = doc.ToXDocument();
var children = xdoc.Document.Element( "parent" ).Elements( "child" );
foreach ( var child in children)
{
Console .WriteLine(child.Value);
}