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 ...
随机推荐
- keil c51的内部RAM(idata)动态内存管理程序
程序比较简单,但感觉比较有意思,个人认为有一定应用价值,希望大家有更好的思路和方法,互相促进. 程序的基本思路是:在CPU堆栈指针SP以上的RAM区域,通过把堆栈指针SP上移若干个字节,把空出的RAM ...
- Android中ListView嵌套GridView的简单消息流UI(解决宽高问题)
最近搞一个项目,需要用到类似于新浪微博的消息流,即每一项有文字.有九宫格图片,因此这就涉及到ListView或者ScrollView嵌套GridView的问题.其中GridView的高度问题在网上都很 ...
- The Flat Dictionary
The Flat Dictionary 原来的代码没处理dict为空的情况 1 def flatten(dictionary): 2 #[] is a list 3 #() is a tuple 4 ...
- IOS回调机制——代理,通知中心以及Block
Xcode5.0正式版 IOS7和Xcode5正式版在昨天正式可以下载.IOS7不多说了,交互设计,界面风格,操作的简化程度都属于比较领先的水平. 这里来说说Xcode5正式版,和以前的Xcode5测 ...
- jquery 弹出框 showDialog.js具体用法
aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAABWwAAAImCAIAAABID1T7AAAgAElEQVR4nO3d329c52Hgff1HvPCNLw
- mybatis于Date和DateTime现场插入
最近,该公司使用MyBatis3做数据持久层,有在该领域Date和DateTime种类,只有在插入数据时属性设置为一个实体Timestamp将相应mysql的DateTime类型.Date会相应mys ...
- 机器学习实战笔记5(logistic回归)
1:简单概念描写叙述 如果如今有一些数据点,我们用一条直线对这些点进行拟合(改线称为最佳拟合直线),这个拟合过程就称为回归.训练分类器就是为了寻找最佳拟合參数,使用的是最优化算法. 基于sigmoid ...
- Android 打造自己的个性化应用(一):应用程序换肤主流方式的分析与概述
Android平台api没有特意为换肤提供一套简便的机制,这可能是外国的软件更注重功能和易用,不流行换肤.系统不提供直接支持,只能自行研究. 换肤,可以认为是动态替换资源(文字.颜色.字体大小.图片. ...
- 写一个简易web服务器、ASP.NET核心知识(4)--转载
第一次尝试(V1.0) 1.理论支持 这里主要要说的关于Socket方面的.主要是一个例子,关于Socket如何建立服务端程序的简单的代码. static void Main(string[] arg ...
- Table表格的一些操作
首先创建一个table表格: <input type="button" id="btn1" value="获取数据" /> &l ...