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 ...
随机推荐
- Poj3468-A Simple Problem with Integers(伸展树练练手)
Description You have N integers, A1, A2, ... , AN. You need to deal with two kinds of operations. On ...
- Linux usb子系统(一):子系统架构
一.USB协议基础知识 前序:USB概念概述 USB1.0版本速度1.5Mbps(低速USB) USB1.1版本速度12Mbps(全速USB) USB2.0版本速度480Mbps(高速USB). ...
- DoTween学习笔记(二) UGUI结合使用(实现一些简单效果)
UGUI官方实例中是使用Animation来控制UI的移动,放大缩小动画等等, Animation来控制UI的动画工作量实在是太多了, 所以我们一般使用itween,DoTween. 来控制动画, 这 ...
- 高仿拉手网底部菜单实现FragmentActivity+Fragment+RadioGroup
先把欢迎页和引导页的代码上传了http://download.csdn.net/detail/u013134391/7183787不要积分的. 底部菜单条实如今4.0曾经都是用tabhost.如今基本 ...
- baidu地图让多个标注出现在最佳视野
原文:http://www.cnblogs.com/milkmap/archive/2011/08/23/2150641.html 摘要: “我有一堆标注,不规则的散落在地图的各个地方,我想把它们展示 ...
- webservice和.net remoting浅谈
服务器端向客户端发送一个进程编号,一个程序域编号,以确定对象的位置. webservice和.net remoting都是用来通信的框架,它们最大的优点是可以像调用本地对象一样调用远程对象,比如: ...
- asp.net repeater控件操作
Repeater控件和DataList控件,可以用来一次显示一组数据项.比如,可以用它们显示一个数据表中的所有行. Repeater控件完全由模板驱动,提供了最大的灵活性,可以任意设置它的输出格式. ...
- java合并list
import java.util.ArrayList; import java.util.List; import com.google.common.collect.Lists; priva ...
- VS2010程序打包操作(超详细的)
1. 在vs2010 选择“新建项目”----“其他项目类型”----“Visual Studio Installerà“安装项目”: 命名为:Setup1 . 这是在VS2010中将有三个文件夹, ...
- BZOJ 2176 Strange String (最小表示法)
题目大意: 与别的裸题的唯一不同点是其符号的ASCII码值在3 ~ 254 之间. 算法讨论: 最小表示法直接上.但是唯一不同的就是注意这里的字符范围,用char是会get wa的,所以要用unsig ...