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. 解决no declaration can be found for element 'context:component-scan'

    <?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.spr ...

  2. Android——Activity的生命周期

    一,Demo測试Activity的生命周期 写两个Activity: package com.example.activity_04; import android.os.Bundle; import ...

  3. Json,String,Map之间的转换

    前提是String的格式是map或json类型的 String 转Json JSONObject  jasonObject = JSONObject.fromObject(str); String 转 ...

  4. POJ 2029 Get Many Persimmon Trees (二维树状数组)

    Get Many Persimmon Trees Time Limit:1000MS    Memory Limit:30000KB    64bit IO Format:%I64d & %I ...

  5. Python魔法师

    第一章:数据结构和算法 1.1 查找最大或者最小的n个元素 heapq 模块的两个函数 nlargest()  nsmallest() import heapq nums = [1, 8, 2, 23 ...

  6. 【Android开发-6】了解内情,我们须要一些调试和測试手段

    前言:人生不可能十全十美,总会有些遗憾存在,经历过遗憾,我们才懂的什么是生活. 程序也一样.追求完美,就必定会有经历bug存在的时候. 经历过不断的bug磨练.我们技术才会不断的成长.对于调试bug, ...

  7. 转: Appium ---移动自动化测试

    转自:http://www.cnblogs.com/nbkhic/p/3803830.html 什么是appium? 下面这段介绍来自于appium的官网. Appium is an open-sou ...

  8. Atitit。Time base gc 垃圾 资源 收集的原理与设计

    Atitit.Time base gc 垃圾 资源 收集的原理与设计 1. MRC(MannulReference Counting手动 retain/release/autorelease语句1 2 ...

  9. JAVA问题之泛型数组

      java中类似下面的代码编译器是会报错的: LinkedList<LinkedList<String>>[] li=new LinkedList<LinkedList ...

  10. ckediter一些小的问题解决办法

    上传图片后,不能看到缩略图. 解决的方法是: BaseDir = "";Thumbnails.Dir = BaseDir + "_thumbs/"; 改为 Th ...