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.     ));  


 

原文地址:https://www.cnblogs.com/micro-chen/p/5663243.html