分享

XmlDocument,XDocument相互转换

 轻应用开发 2014-01-27

XmlDocument,XDocument相互转换


  1. using System;  
  2. using System.Xml;  
  3. using System.Xml.Linq;  
  4.   
  5. namespace MyTest  
  6. {  
  7.     internal class Program  
  8.     {  
  9.         private static void Main(string[] args)  
  10.         {  
  11.   
  12.             var xmlDocument = new XmlDocument();  
  13.             xmlDocument.LoadXml("<Root><Child>Test</Child></Root>");  
  14.   
  15.             var xDocument = xmlDocument.ToXDocument();  
  16.             var newXmlDocument = xDocument.ToXmlDocument();  
  17.             Console.ReadLine();  
  18.         }  
  19.     }  
  20.   
  21.     public static class DocumentExtensions  
  22.     {  
  23.         public static XmlDocument ToXmlDocument(this XDocument xDocument)  
  24.         {  
  25.             var xmlDocument = new XmlDocument();  
  26.             using(var xmlReader = xDocument.CreateReader())  
  27.             {  
  28.                 xmlDocument.Load(xmlReader);  
  29.             }  
  30.             return xmlDocument;  
  31.         }  
  32.   
  33.         public static XDocument ToXDocument(this XmlDocument xmlDocument)  
  34.         {  
  35.             using (var nodeReader = new XmlNodeReader(xmlDocument))  
  36.             {  
  37.                 nodeReader.MoveToContent();  
  38.                 return XDocument.Load(nodeReader);  
  39.             }  
  40.         }  
  41.     }  
  42. }  



如果您正在使用3.0或更低,您必须使用XmlDocument aka经典的DOM API。同样地,你会发现有一些其他api可以期待


如果你想要选择,我将彻底推荐使用LINQ to XML XDocument aka。这是更简单的创建文件和处理它们。例如,它的区别


  1. XmlDocument doc = new XmlDocument();  
  2. XmlElement root = doc.CreateElement("root");  
  3. root.SetAttribute("name""value");  
  4. XmlElement child = doc.CreateElement("child");  
  5. child.InnerText = "text node";  
  6. root.AppendChild(child);  
  7. doc.AppendChild(root);  
  8. and  
  9.   
  10. XDocument doc = new XDocument(  
  11.     new XElement("root",  
  12.                  new XAttribute("name""value"),  
  13.                  new XElement("child""text node")));  



 


Namespaces are pretty easy to work with in LINQ to XML, unlike any other XML API I've ever seen:


  1. XNamespace ns = "http://";  
  2. XElement element = new XElement(ns + "elementName");  
  3. // etc  

LINQ to XML also works really well with LINQ - its construction model allows you to build elements with sequences of sub-elements really easily:


  1. // Customers is a List<Customer>  
  2. XElement customersElement = new XElement("customers",  
  3.     customers.Select(c => new XElement("customer",  
  4.         new XAttribute("name", c.Name),  
  5.         new XAttribute("lastSeen", c.LastOrder)  
  6.         new XElement("address",  
  7.             new XAttribute("town", c.Town),  
  8.             new XAttribute("firstline", c.Address1),  
  9.             // etc  
  10.     ));  



 



    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多