格式化xml

格式化xml打开xml:格式化xml:3.获取xml内容:源码下载

大家好,欢迎来到IT知识分享网。格式化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 = '\t';
                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;
        }

源码下载

免责声明:本站所有文章内容,图片,视频等均是来源于用户投稿和互联网及文摘转载整编而成,不代表本站观点,不承担相关法律责任。其著作权各归其原作者或其出版社所有。如发现本站有涉嫌抄袭侵权/违法违规的内容,侵犯到您的权益,请在线联系站长,一经查实,本站将立刻删除。 本文来自网络,若有侵权,请联系删除,如若转载,请注明出处:https://yundeesoft.com/33987.html

(0)

相关推荐

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注微信