• 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;
    }
    

      

  • 相关阅读:
    Java web学习总结
    java web知识点
    SSH进阶之路
    file /etc/httpd/conf.d/php.conf from install of php-5.6.37-1.el7.remi.x86_64 conflicts with file from package mod_php71w-7.1.18-1.w7.x86_64
    centos7上安装php5.6
    centos7 删除php
    centos7删除yum安装的php
    给服务器加内存
    Hyperledger Fabric 开发环境搭建 centos7系统
    79
  • 原文地址:https://www.cnblogs.com/XuPengLB/p/5587263.html
Copyright © 2020-2023  润新知