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. sort详解

    一. 简介 sort命令是帮我们依据不同的数据类型进行排序. 二. 语法 sort [-bcfMnrtk][源文件][-o 输出文件] 补充说明:sort可针对文本文件的内容,以行为单位来排序(默认为 ...

  2. 缓存一致性(Cache Coherency)入门(转)

    参考原文:http://fgiesen.wordpress.com/2014/07/07/cache-coherency/ 本文是RAD Game Tools程序员Fabian “ryg” Giese ...

  3. windows中.msc文件详解

    msc是Microsoft Management Console的缩写.其实是一种可执行程序类型,可.exe类似.一般可以通过直接双击.msc文件或者在windows的运行中输入相应的文件名来启动. ...

  4. Sum Root to Leaf Numbers 解答

    Question Given a binary tree containing digits from 0-9 only, each root-to-leaf path could represent ...

  5. Longest Common Prefix 解答

    Question Write a function to find the longest common prefix string amongst an array of strings. Solu ...

  6. Longest Palindromic Substring 解答

    Question Given a string S, find the longest palindromic substring in S. You may assume that the maxi ...

  7. Zoj3332-Strange Country II(有向竞赛图)

    You want to visit a strange country. There are n cities in the country. Cities are numbered from 1 t ...

  8. <转载>构造函数与拷贝构造函数

    原文地址http://www.cnblogs.com/waynelu/archive/2012/07/01/2572264.html 构造函数 构造函数.析构函数与赋值函数是每个类最基本的函数. 对于 ...

  9. paip.输入法编程---智能动态上屏码儿长调整--.txt

    paip.输入法编程---智能动态上屏码儿长调整--.txt 作者Attilax ,  EMAIL:1466519819@qq.com 来源:attilax的专栏 地址:http://blog.csd ...

  10. URL重写是实现PHP伪静态

    URL重写是实现PHP伪静态 应该这样说才是,URL重写是实现PHP伪静态的方式之一, 具体如: http://www.plframe.com/?x=1&y=2&z=3 换成 http ...