1:XDocument转String直接使用ToString();XNode里面重写了ToString()方法

2:XmlDocument转String需要写代码

 using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml;
using System.Xml.Linq;
using System.IO; namespace XmlToString
{
class Program
{
static void Main(string[] args)
{
var xDoc = new XDocument(
new XElement("root111",
new XElement("dog",
new XText("哈哈哈"),
new XAttribute("color", "black")
),
//new XElement("cat"),
new XElement("pig", "pig is great")
));
Console.WriteLine(xDoc.ToString());
string strXml = "<?xml version=\"1.0\" encoding=\"utf-8\"?><books><book>1</book><book>2</book><book>3</book><book>4</book></books>";
XmlDocument xx = new XmlDocument();
xx.LoadXml(strXml);
Console.WriteLine(xx.ToString());
StringWriter sw = new StringWriter();
XmlTextWriter xmlTextWriter = new XmlTextWriter(sw);
xx.WriteTo(xmlTextWriter);
Console.WriteLine(sw.ToString());
Console.ReadKey();
}
}
}

XmlDocument和XDocument转String的更多相关文章

  1. XmlDocument,XDocument相互转换

    XmlDocument,XDocument相互转换 using System; using System.Xml; using System.Xml.Linq; namespace MyTest { ...

  2. C# -- 使用XmlDocument或XDocument创建xml文件

    使用XmlDocument或XDocument创建xml文件 需引用:System.Xml; System.Xml.Linq; 1.使用XmlDocument创建xml(入门案例) static vo ...

  3. XmlValidationHelper XSD、Schema(XmlSchemaSet)、XmlReader(XmlValidationSettings)、XmlDocument、XDocument Validate

    namespace Test { using Microshaoft; using System; using System.Xml; using System.Xml.Linq; class Pro ...

  4. XmlDocument To String

    一.从String xml到XmlDocument的: string xml = "<XML><Test>Hello World</Test></X ...

  5. XmlDocument.selectNodes() and selectSingleNode()的xpath的学习资料

    Xpath网页: http://www.w3school.com.cn/xpath/xpath_syntax.asp XDocument.parse(string)类似于XmlDocument.loa ...

  6. 七、Linq To XML:XElement、XDocument

    一.概述 LINQ to XMLLINQ to XML 是一种启用了 LINQ 的内存 XML 编程接口,使用它,可以在 .NET Framework.NET Framework 编程语言中处理 XM ...

  7. XML 之 与Json或String的相互转换

    1.XML与String的相互转换 [1] XML 转为 String //载入Xml文件 XmlDocument xdoc = new XmlDocument(); xdoc.Load(" ...

  8. C# 使用XmlDocument类对XML文档进行操作

    原创地址:http://www.cnblogs.com/jfzhu/archive/2012/11/19/2778098.html 转载请注明出处 W3C制定了XML DOM标准.很多编程语言中多提供 ...

  9. C# 操作XML文档 使用XmlDocument类方法

    W3C制定了XML DOM标准.很多编程语言中多提供了支持W3C XML DOM标准的API.我在之前的文章中介绍过如何使用Javascript对XML文档进行加载与查询.在本文中,我来介绍一下.Ne ...

随机推荐

  1. js获取页面传来参数的方法

    function GetQueryString(name) { var reg = new RegExp("(^|&)" + name + "=([^&] ...

  2. Solr 创建core 从MySql数据库中导入数据

    一.创建数据表和数据 在MySql数据中创建mysolrInfo表, 创建字段 id 主键,自动增加 pname :姓名 age :年龄 addtime :增加时间 增加几条数据 二.创建core 当 ...

  3. [Functional Programming] Compose Simple State ADT Transitions into One Complex Transaction

    State is a lazy datatype and as such we can combine many simple transitions into one very complex on ...

  4. Spring声明式事务的配置方式

    1.事务的特性   原子性:事务中的操作是不可分割的一部分   一致性:要么同时成功,要么同时失败(事务执行前后数据保持一致)   隔离性:并发互不干扰     持久性:事务一旦被提交,它就是一条持久 ...

  5. Solidworks 如何快速完全定义草图

    工具-尺寸标注-完全定义草图

  6. pip 安装自己开发模块 边调试边修改

    pip install -e /path/to/mypackage

  7. Android学习(十八)Toast的使用

    一.什么是Toast 1.Toast是一种提供给用户简洁提示信息的视图. 2.该视图以浮于应用程序之上的形式呈现给用户, Toast提示界面不获取焦点,在不影响用户使用的情况下,给用户某些提示. 3. ...

  8. Laravel之加密解密/日志/异常处理及自定义错误

    一.加密解密 1.加密Crypt::encrypt($request->secret) 2.解密try { $decrypted = Crypt::decrypt($encryptedValue ...

  9. Laravel之目录结构

    一.根目录 新安装的 Laravel 应用包含许多文件夹:• app 目录包含了应用的核心代码:• bootstrap 目录包含了少许文件用于框架的启动和自动载入配置,还有一个cache 文件夹用于包 ...

  10. 迭代器适配器(二)general inserter的实现

    上节我们实现了back_inserter和front_inserter,接下来是更为普通的插入迭代器,它允许用户指定插入位置. 实现代码如下: #ifndef ITERATOR_HPP #define ...