zoukankan      html  css  js  c++  java
  • 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 }
  • 相关阅读:
    MySQL灾备切换
    crontab 定时任务
    Mysql常用命令 详细整理版
    linux 常用命令
    shell逻辑运算总结, 包括[[]]与[]的区别,&&与-a的区别,||与-o的区别
    linux端口详解大全
    编译安装php5.6
    linux给用户添加sudo权限
    Effective C#(二)
    Effective C#(一)
  • 原文地址:https://www.cnblogs.com/lip-blog/p/7693344.html
Copyright © 2011-2022 走看看