从 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

posted @ 2010-07-02 11:23  山村果园  阅读(788)  评论(0编辑  收藏  举报