• 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 }
  • 相关阅读:
    [BZOJ3413]匹配
    [BZOJ3879]SvT
    [BZOJ1339] [Baltic2008] Mafia / 黑手党
    [BZOJ1834] [ZJOI2010] network 网络扩容
    [BZOJ1449] [JSOI2009]球队收益 / [BZOJ2895] 球队预算
    [BZOJ2597] [WC2007]剪刀石头布
    [BZOJ1283]序列
    [BZOJ2127]happiness
    Java公开课-04.异常
    Java公开课-03.内部类
  • 原文地址:https://www.cnblogs.com/lip-blog/p/7693344.html
Copyright © 2020-2023  润新知