XmlDocument,XDocument相互转换
XmlDocument,XDocument相互转换
- using System;
- using System.Xml;
- using System.Xml.Linq;
- namespace MyTest
- {
- internal class Program
- {
- private static void Main(string[] args)
- {
- var xmlDocument = new XmlDocument();
- xmlDocument.LoadXml("<Root><Child>Test</Child></Root>");
- var xDocument = xmlDocument.ToXDocument();
- var newXmlDocument = xDocument.ToXmlDocument();
- Console.ReadLine();
- }
- }
- public static class DocumentExtensions
- {
- public static XmlDocument ToXmlDocument(this XDocument xDocument)
- {
- var xmlDocument = new XmlDocument();
- using(var xmlReader = xDocument.CreateReader())
- {
- xmlDocument.Load(xmlReader);
- }
- return xmlDocument;
- }
- public static XDocument ToXDocument(this XmlDocument xmlDocument)
- {
- using (var nodeReader = new XmlNodeReader(xmlDocument))
- {
- nodeReader.MoveToContent();
- return XDocument.Load(nodeReader);
- }
- }
- }
- }
如果您正在使用3.0或更低,您必须使用XmlDocument aka经典的DOM API。同样地,你会发现有一些其他api可以期待
如果你想要选择,我将彻底推荐使用LINQ to XML XDocument aka。这是更简单的创建文件和处理它们。例如,它的区别
- XmlDocument doc = new XmlDocument();
- XmlElement root = doc.CreateElement("root");
- root.SetAttribute("name", "value");
- XmlElement child = doc.CreateElement("child");
- child.InnerText = "text node";
- root.AppendChild(child);
- doc.AppendChild(root);
- and
- XDocument doc = new XDocument(
- new XElement("root",
- new XAttribute("name", "value"),
- 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:
- XNamespace ns = "http://somewhere.com";
- XElement element = new XElement(ns + "elementName");
- // 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:
- // Customers is a List<Customer>
- XElement customersElement = new XElement("customers",
- customers.Select(c => new XElement("customer",
- new XAttribute("name", c.Name),
- new XAttribute("lastSeen", c.LastOrder)
- new XElement("address",
- new XAttribute("town", c.Town),
- new XAttribute("firstline", c.Address1),
- // etc
- ));
XmlDocument,XDocument相互转换的更多相关文章
- C# XML技术总结之XDocument 和XmlDocument
引言 虽然现在Json在我们的数据交换中越来越成熟,但XML格式的数据还有很重要的地位. C#中对XML的处理也不断优化,那么我们如何选择XML的这几款处理类 XmlReader,XDocument ...
- XML技术总结之XDocument 和XmlDocument
引言 虽然现在Json在我们的数据交换中越来越成熟,但XML格式的数据还有很重要的地位. C#中对XML的处理也不断优化,那么我们如何选择XML的这几款处理类 XmlReader,XDocument ...
- Binding笔记
Binding基础 绑定某个对象的属性值到控制上,写法如下: public class Order : INotifyPropertyChanged//只要实现此接口 { public event ...
- Binding
Binding基础 绑定某个对象的属性值到控制上,写法如下: public class Order : INotifyPropertyChanged//只要实现此接口 { public event ...
- .NETCore3.1中的Json互操作最全解读-收藏级
前言 本文比较长,我建议大家先点赞.收藏后慢慢阅读,点赞再看,形成习惯! 我很高兴,.NETCore终于来到了3.1LTS版本,并且将支持3年,我们也准备让部分业务迁移到3.1上面,不过很快我们就遇到 ...
- C#加载XML方式
//path:xml文件路径 SECSMessage:xml文件的根元素下的第一个子集元素 <SECSLibrary> <SECSMessage> <Descripti ...
- Roslyn+T4+EnvDTE项目完全自动化 (一)
前言 以前做一个金融软件项目,软件要英文.繁体版本,开始甲方弄了好几个月,手动一条一条替换,发现很容易出错,因为有金融专业术语,字符串在不同语义要特殊处理,第三方工具没法使用.最后我用Roslyn写了 ...
- XmlValidationHelper XSD、Schema(XmlSchemaSet)、XmlReader(XmlValidationSettings)、XmlDocument、XDocument Validate
namespace Test { using Microshaoft; using System; using System.Xml; using System.Xml.Linq; class Pro ...
- 将XmlDocument转换成XDocument
XmlDocument xml=new XmlDocument(); xml.LoadXml(strXmlText); XmlReader xr=new XmlNodeReader(xml); XDo ...
随机推荐
- CRC校验源码分析
这两天做项目,需要用到 CRC 校验.以前没搞过这东东,以为挺简单的.结果看看别人提供的汇编源程序,居然看不懂.花了两天时间研究了一下 CRC 校验,希望我写的这点东西能够帮助和我有同样困惑的朋友节省 ...
- QProcess进程间双向通信
记得以前写过Linux的C程序, 里面用popen打开一个子进程, 这样可以用read/write和子进程通讯, 而在子进程里则是通过从stdin读和向stdout写实现对父进程的通讯. QProce ...
- Android添加横线和竖线分割界面
竖线 <View android:layout_width="1dip" android:layout_height="match_parent ...
- Linux文件夹执行权限
在Linux中,文件有三种权限--可读,可写,可执行.目录也有三种权限--可读,可写,可执行.但是实际上他们有着不同的意义. 对于文件: 可读 :表示可以读取文件里的数据: 可写 :表示可以改变和删除 ...
- 商派shopex
http://www.shopex.cn/48release/shopexsingle_exper.php 在线体验 前台体验:http://demo.shopex.com.cn/485 后台体验:h ...
- 怎样合并排序数组(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 ...
- Yii2 分页类的扩展和listview引用
Yii2 本身提供了不错分页选项供用户设置,但是实际项目中我们往往需要复杂一些的分页样式,例如下图所示的效果,上下翻页可用和不可用均用图标来替换.
- [转载]用可变参数宏(variadic macros)传递可变参数表
注意:_VA_ARGS__ 从VS2005才开始支持 在 GNU C 中,宏可以接受可变数目的参数,就象函数一样,例如: #define pr_debug(fmt,arg...) printk(KER ...
- 《Java程序员面试笔试宝典》之Java程序初始化的顺序是怎样的
在Java语言中,当实例化对象时,对象所在类的所有成员变量首先要进行初始化,只有当所有类成员完成初始化后,才会调用对象所在类的构造函数创建对象. Java程序的初始化一般遵循以下三个原则(以下三原则优 ...
- php 的设计模式
1.单例模式 单例模式顾名思义,就是只有一个实例.作为对象的创建模式, 单例模式确保某一个类只有一个实例,而且自行实例化并向整个系统提供这个实例. 单例模式的要点有三个: 一是某个类只能有一个实例: ...