从 XmlDocument到XDocument的转换
扩展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);
}
摘自:http://www.cnblogs.com/wangsu/archive/2010/03/31/1701849.html