XmlDocument,XDocument相互转换

XmlDocument,XDocument相互转换

[csharp]  view plain  copy
 
  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。这是更简单的创建文件和处理它们。例如,它的区别

[csharp]  view plain  copy
 
  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:

[csharp]  view plain  copy
 
  1. XNamespace ns = "http://somewhere.com";  
  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:

[csharp]  view plain  copy
 
  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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值