zoukankan      html  css  js  c++  java
  • 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.     ));  


     

  • 相关阅读:
    20.C语言_数组参数传递
    19.C语言_取值运算符*
    Google Chrome 解决 “您的连接不是私密连接” 和被毒霸劫持
    教你如何一步步将项目部署到Github
    教你如何把Android手机的网络完全映射到PC上,比如免流给PC用…
    CSS background-size 属性详解
    display:block;是什么意思
    Cookie是储存在电脑文本文件中的数据,用于保存访问者的信息,并可以在下次打开页面时引用。
    Marquee(跑马灯)横向、纵向、无空白的不间断连续循环滚动代码
    Visual Studio中从应用程序中调试SQL脚本
  • 原文地址:https://www.cnblogs.com/micro-chen/p/5663243.html
Copyright © 2011-2022 走看看