zoukankan      html  css  js  c++  java
  • 格式化xml

    打开xml:

            string xmlstring = "";
            private void button1_Click(object sender, EventArgs e)
            {
                OpenFileDialog dialog = new OpenFileDialog();
                dialog.InitialDirectory = Application.StartupPath;
                dialog.Filter = "All Files|*.*|xml file(*.xml)|*.xml";
                dialog.RestoreDirectory = true;
                if (dialog.ShowDialog() == DialogResult.OK)
                {
                    string filepath = dialog.FileName.ToString();
                    string filename = filepath.Substring(filepath.LastIndexOf("\") + 1);
                    textBox1.Text = filepath;
                    using (StreamReader sr = new StreamReader(filepath, Encoding.Default))//如果乱码,可改为System.Text.Encoding.UTF-8;或者System.Text.Encoding.GBK
                    {
                        string str;
                        string line = "";
                        while ((str = sr.ReadLine()) != null)
                        {
                            line += str;
                        }
                        xmlstring = line;
                    }
                }
                richTextBox1.Text = xmlstring;
            }

    格式化xml:

            private string FormatXml(string sUnformattedXml)
            {
                StringReader Reader = new StringReader(sUnformattedXml);
                XmlDocument xd = new XmlDocument();
                xd.Load(Reader);
                StringBuilder sb = new StringBuilder();
                StringWriter sw = new StringWriter(sb);
                XmlTextWriter xtw = null;
                try
                {
                    xtw = new XmlTextWriter(sw);
                    xtw.Formatting = Formatting.Indented;
                    xtw.Indentation = 1;
                    xtw.IndentChar = '	';
                    xd.WriteTo(xtw);
                }
                finally
                {
                    if (xtw != null)
                        xtw.Close();
                }
                return sb.ToString();
            }

    3.获取xml内容:

            public string AllChildNode(string xml)
            {
                StringReader Reader = new StringReader(xml);
                XmlDocument doc = new XmlDocument();
                doc.Load(Reader);
                string str = "";
                XmlNode root = doc.FirstChild;
                if (root.HasChildNodes)
                {
                    for (int i = 0; i < root.ChildNodes.Count; i++)
                    {
                        str += root.ChildNodes[i].InnerText;
                    }
                }
                return str;
            }

    源码下载

  • 相关阅读:
    请求转发和请求重定向的区别
    查看电脑连过的WiFi密码
    linux mysql不能远程登录
    map的遍历方法
    ________________springbootのMybatis
    ________________springbootのTest
    ________________springbootの自定义starter
    ________________springbootのAOP
    ________________springbootのjdbc、事物
    ________________初学springboot14
  • 原文地址:https://www.cnblogs.com/ytwy/p/5515799.html
Copyright © 2011-2022 走看看