XmlDocument,XDocument相互转换

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

如果您正在使用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. XDocument doc = new XDocument(
  10. new XElement("root",
  11. new XAttribute("name", "value"),
  12. 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://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:

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

XmlDocument,XDocument相互转换的更多相关文章

  1. C# XML技术总结之XDocument 和XmlDocument

    引言 虽然现在Json在我们的数据交换中越来越成熟,但XML格式的数据还有很重要的地位. C#中对XML的处理也不断优化,那么我们如何选择XML的这几款处理类 XmlReader,XDocument ...

  2. XML技术总结之XDocument 和XmlDocument

    引言 虽然现在Json在我们的数据交换中越来越成熟,但XML格式的数据还有很重要的地位. C#中对XML的处理也不断优化,那么我们如何选择XML的这几款处理类 XmlReader,XDocument ...

  3. Binding笔记

    Binding基础  绑定某个对象的属性值到控制上,写法如下: public class Order : INotifyPropertyChanged//只要实现此接口 { public event ...

  4. Binding

    Binding基础  绑定某个对象的属性值到控制上,写法如下: public class Order : INotifyPropertyChanged//只要实现此接口 { public event  ...

  5. .NETCore3.1中的Json互操作最全解读-收藏级

    前言 本文比较长,我建议大家先点赞.收藏后慢慢阅读,点赞再看,形成习惯! 我很高兴,.NETCore终于来到了3.1LTS版本,并且将支持3年,我们也准备让部分业务迁移到3.1上面,不过很快我们就遇到 ...

  6. C#加载XML方式

    //path:xml文件路径  SECSMessage:xml文件的根元素下的第一个子集元素 <SECSLibrary> <SECSMessage> <Descripti ...

  7. Roslyn+T4+EnvDTE项目完全自动化 (一)

    前言 以前做一个金融软件项目,软件要英文.繁体版本,开始甲方弄了好几个月,手动一条一条替换,发现很容易出错,因为有金融专业术语,字符串在不同语义要特殊处理,第三方工具没法使用.最后我用Roslyn写了 ...

  8. XmlValidationHelper XSD、Schema(XmlSchemaSet)、XmlReader(XmlValidationSettings)、XmlDocument、XDocument Validate

    namespace Test { using Microshaoft; using System; using System.Xml; using System.Xml.Linq; class Pro ...

  9. 将XmlDocument转换成XDocument

    XmlDocument xml=new XmlDocument(); xml.LoadXml(strXmlText); XmlReader xr=new XmlNodeReader(xml); XDo ...

随机推荐

  1. CRC校验源码分析

    这两天做项目,需要用到 CRC 校验.以前没搞过这东东,以为挺简单的.结果看看别人提供的汇编源程序,居然看不懂.花了两天时间研究了一下 CRC 校验,希望我写的这点东西能够帮助和我有同样困惑的朋友节省 ...

  2. QProcess进程间双向通信

    记得以前写过Linux的C程序, 里面用popen打开一个子进程, 这样可以用read/write和子进程通讯, 而在子进程里则是通过从stdin读和向stdout写实现对父进程的通讯. QProce ...

  3. Android添加横线和竖线分割界面

    竖线 <View       android:layout_width="1dip"     android:layout_height="match_parent ...

  4. Linux文件夹执行权限

    在Linux中,文件有三种权限--可读,可写,可执行.目录也有三种权限--可读,可写,可执行.但是实际上他们有着不同的意义. 对于文件: 可读 :表示可以读取文件里的数据: 可写 :表示可以改变和删除 ...

  5. 商派shopex

    http://www.shopex.cn/48release/shopexsingle_exper.php 在线体验 前台体验:http://demo.shopex.com.cn/485 后台体验:h ...

  6. 怎样合并排序数组(How to merge 2 sorted arrays?)

    Question: We have 2 sorted arrays and we want to combine them into a single sorted array. Input: arr ...

  7. Yii2 分页类的扩展和listview引用

    Yii2 本身提供了不错分页选项供用户设置,但是实际项目中我们往往需要复杂一些的分页样式,例如下图所示的效果,上下翻页可用和不可用均用图标来替换.

  8. [转载]用可变参数宏(variadic macros)传递可变参数表

    注意:_VA_ARGS__ 从VS2005才开始支持 在 GNU C 中,宏可以接受可变数目的参数,就象函数一样,例如: #define pr_debug(fmt,arg...) printk(KER ...

  9. 《Java程序员面试笔试宝典》之Java程序初始化的顺序是怎样的

    在Java语言中,当实例化对象时,对象所在类的所有成员变量首先要进行初始化,只有当所有类成员完成初始化后,才会调用对象所在类的构造函数创建对象. Java程序的初始化一般遵循以下三个原则(以下三原则优 ...

  10. php 的设计模式

    1.单例模式 单例模式顾名思义,就是只有一个实例.作为对象的创建模式, 单例模式确保某一个类只有一个实例,而且自行实例化并向整个系统提供这个实例. 单例模式的要点有三个: 一是某个类只能有一个实例: ...