XmlDocument和XDocument转String


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

2:XmlDocument转String需要写代码

 

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 using System.Threading.Tasks;
 6 using System.Xml;
 7 using System.Xml.Linq;
 8 using System.IO;
 9 
10 namespace XmlToString
11 {
12     class Program
13     {
14         static void Main(string[] args)
15         {
16             var xDoc = new XDocument(
17                 new XElement("root111",
18                     new XElement("dog",
19                         new XText("哈哈哈"),
20                         new XAttribute("color", "black")
21                         ),
22                     //new XElement("cat"),
23                     new XElement("pig", "pig is great")
24                 ));
25             Console.WriteLine(xDoc.ToString());
26             string strXml = "<?xml version=\"1.0\" encoding=\"utf-8\"?><books><book>1</book><book>2</book><book>3</book><book>4</book></books>";
27             XmlDocument xx = new XmlDocument();
28             xx.LoadXml(strXml);
29             Console.WriteLine(xx.ToString());
30             StringWriter sw = new StringWriter();
31             XmlTextWriter xmlTextWriter = new XmlTextWriter(sw);
32             xx.WriteTo(xmlTextWriter);
33             Console.WriteLine(sw.ToString());
34             Console.ReadKey();
35         }
36     }
37 }

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM