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 }
  • 相关阅读:
    java Math类
    JAVA Date类与Calendar类【转】
    java Runtime类
    Java System类
    java 多线程
    java 包
    Java 内部类
    java 抽象类 以及模块方法设计模式,接口
    java 单例模式
    java 关于Java中静态代码块以及构造函数的执行先后顺序
  • 原文地址:https://www.cnblogs.com/lip-blog/p/7693344.html
Copyright © 2011-2022 走看看