zoukankan      html  css  js  c++  java
  • XmlDocument To String

    一.从String xml到XmlDocument的:

    string xml = "<XML><Test>Hello World</Test></XML>"
    XmlDocument doc = new XmlDocument();
    xml.loadXml(xml);
    

    二.将XmlDocument内容 转换成String xml

    //(1)如果只要求字符串的话用xmlDocument 的 OuterXml 方法就可以实现:
    doc.OutXml;
    
    //(2)转字节流的方式
    //这种方式可以把 xmlDocument 内容变成比较符合标准格式的 xml 字符串,例如:
    //<?xml version="1.0" encoding="utf-8"?>
    //<XML>
    //  <Test>Hello World</Test>
    //</XML>
    // xmlDocument to string
    public static string xmlDocument2String(XmlDataDocument doc)
    {
                MemoryStream stream = new MemoryStream();
                XmlTextWriter writer = new XmlTextWriter(stream, System.Text.Encoding.UTF8);
                writer.Formatting = Formatting.Indented;
                doc.Save(writer);
    
                StreamReader sr = new StreamReader(stream, System.Text.Encoding.UTF8);
                stream.Position = 0;
                string xmlstring = sr.ReadToEnd();
                sr.Close();
                stream.Close();
    
                return xmlstring;
    }
    
    //(3)先写到本地文件,再字符串形式流读取:
    public string XmltoString(string path)
    {
                string strXML = "";
                string strLine = "";
                StreamReader objReader = new StreamReader(path);
                // read line
                while ((strLine = objReader.ReadLine()) != null)
                {
                    strXML += strLine;
                }
                objReader.Close();
    
                return strXML;
    }
    

      

  • 相关阅读:
    LeetCode OJ Remove Duplicates from Sorted Array II
    LeetCode OJ 75. Sort Colors
    LeetCode OJ 74. Search a 2D Matrix
    LeetCode OJ 73. Set Matrix Zeroes
    Taglist
    NERDTree
    Beyond Compare 4
    Beyond compare vs kdiff3
    切換 java compiler 版本
    PE+ 1.0 ( Pump Express Plus 1.0 )
  • 原文地址:https://www.cnblogs.com/XuPengLB/p/5587263.html
Copyright © 2011-2022 走看看