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 ...
随机推荐
- Qt5+VS2013兼容XP方法
用Qt5+VS2013编译程序默认配置会在XP运行时报"不是有效的Win32程序"工作需要必须要XP运行 pro文件中加一句: 复制代码 QMAKE_LFLAGS_WINDOWS ...
- idea
一. 常用快捷键 搜索class Ctrl+N 搜索文件 Ctrl+Alt+N 当前窗口查找/全工程查找 Ctrl+F/Ctrl+Shift+F,F3/Shift+F3前后移动 上/下一个位置 Ctr ...
- boost库使用:vs2013下boost::container::vector编译出错解决
boost版本:boost_1_55_0 bug报告地址 https://svn.boost.org/trac/boost/ticket/9332 出错信息 has_member_function_c ...
- MySQL数据备份之mysqldump
mysqldump常用于MySQL数据库逻辑备份 1.各种用法说明 A. 最简单的用法: mysqldump -uroot -pPassword [database name] > [dum ...
- c++智能指针《二》 std::tr1::shared_ptr
转载http://www.cnblogs.com/kadinzhu/archive/2011/12/12/2284826.html 看<effective c++>,作者一直强调用std: ...
- hdu 5288 OO’s Sequence(计数)
Problem Description OO has got a array A of size n ,defined a function f(l,r) represent the number o ...
- C#中,表达式的计算遵循一个规律:从左到右依次计算。
int i = 0; int j = (i++)+(i++)=(i++)+i=i+++i=i+++i++=1;
- pyqt tabWidget例子学习1
from PyQt4 import QtGui from PyQt4 import QtCore from PyQt4.QtCore import pyqtSlot,SIGNAL,SLOT impor ...
- java与.net比较学习系列(7) 属性
文章摘自:http://www.cnblogs.com/mcgrady/p/3411405.html 说起属性,实际上java中没有属性这个概念,只有字段和方法,但是可以通过私有字段和声明get,se ...
- Linux入门基础 #8:Linux拓展权限
本文出自 http://blog.csdn.net/shuangde800 ------------------------------------------------------------ ...