xml与txt文件格式互换

前端之家收集整理的这篇文章主要介绍了xml与txt文件格式互换前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

当前遇到一个问题,需要将txt格式的文件转换为xml格式的文件,网上找了挺多的方法,也成功了,但用时比较麻烦,考虑到后期程序的需要,决定开发一个小程序。耗时两个半天,终于搞定,基本是满足需要,现将主要代码发出,附带编译完可用的exe文件

exe文件下载路径:http://download.csdn.net/detail/bluecy/9575152


 private void button2_Click(object sender,EventArgs e)
        {
               
            string txtfilepath = this.textBoxtxt.Text;
            string xmlfilepath = this.textBoxxml.Text;
            converttoxml(txtfilepath,xmlfilepath);
            //读取xml文件到txtTXT文本框
            StreamReader st = new StreamReader(xmlfilepath,System.Text.Encoding.GetEncoding("GBK"));
               this.textBox.Text = st.ReadToEnd();
               st.Close();
            MessageBox.Show("转换完成!");
        }

        private void button1_Click(object sender,EventArgs e)
        {
            string txtfilepath = this.textBoxtxt.Text;
            string xmlfilepath = this.textBoxxml.Text;
            converttotxt(txtfilepath,xmlfilepath);
            //读取txt文件到txtTXT文本框
            StreamReader st = new StreamReader(txtfilepath,System.Text.Encoding.GetEncoding("GBK"));
            this.textBox.Text = st.ReadToEnd();
            st.Close();
            MessageBox.Show("转换完成!");
        }

        private void converttoxml(String txtfileName,String xmlfileName)
        {
            try
            {
                //将txt内容分解为行数组
                string[] lines = File.ReadAllLines(txtfileName);
                
                String[] heads = null;
                if (lines != null && lines.Length > 0)
                {
                    //读取第一行数据,该行数据为xml文件的节点描述数据
                    heads = lines[0].Split(new string[] { " " },StringSplitOptions.None);
                    //MessageBox.Show(heads.Length.ToString() + " " + heads[0]);
                }
                //MessageBox.Show(heads[0].ToString());
                StringBuilder sb = new StringBuilder();
                sb.Append("<?xml version=\"1.0\"  encoding=\"gbk\"?>").Append(Environment.NewLine).Append("<dataRoot>").Append(Environment.NewLine);
                //生成xml节点
                //MessageBox.Show(sb.ToString());
               for (int i = 1; i < lines.Length; i++)
                {
                      if (lines[i] == null || lines[i].Trim().Length < 1)
                        {
                        continue;
                        }
                  
                    string[] info = lines[i].Split(new string[] { " " },StringSplitOptions.None);
                   
                    sb.Append(createNode(heads,info));
                }
               
                sb.Append("</dataRoot>");
                StreamWriter sw = new StreamWriter(xmlfileName,false);
             
                sw.WriteLine(sb + " ");
                sw.Close();
                sw.Dispose();
                           
            }
            catch (Exception exp)
            {
                MessageBox.Show(exp.Message);
            }
        }

        private String createNode(String[] head,String[] info)
        {
            StringBuilder sb = new StringBuilder();
            sb.Append("<record>").Append(Environment.NewLine);
            for (int i = 0; i < head.Length; i++)
            {
                sb.Append("<" + head[i] + ">" + info[i] + "</" + head[i] + ">").Append(Environment.NewLine);
            }
            sb.Append("</record>").Append(Environment.NewLine);
            return sb.ToString();
        }

        private void converttotxt(String txtfilepath,String xmlfilepath)
        {
            //xml to dataset
            try
            {
                DataSet xmlds = new DataSet();
                //读取XML到DataSet 
                StreamReader sr = new StreamReader(xmlfilepath,Encoding.Default);
                xmlds.ReadXml(sr);
                sr.Close();
                //dataset to txt
                StreamWriter sw = new StreamWriter(txtfilepath,false,Encoding.Default);
                String DataRow = "";
                for (int i = 0; i < xmlds.Tables[0].Columns.Count; i++) //获取列名 
                {
                    DataRow += xmlds.Tables[0].Columns[i].ColumnName;
                    if (i < xmlds.Tables[0].Columns.Count - 1) DataRow += " ";
                }
                sw.WriteLine(DataRow);
                for (int i = 0; i < xmlds.Tables[0].Rows.Count; i++) //获取数据 
                {
                    DataRow = "";
                    for (int j = 0; j < xmlds.Tables[0].Columns.Count; j++)
                    {
                        DataRow += xmlds.Tables[0].Rows[i][j].ToString();
                        if (j < xmlds.Tables[0].Columns.Count - 1) DataRow += " ";
                    }
                    sw.WriteLine(DataRow);
                }
                sw.Close();
            }
            catch
            {
                MessageBox.Show("XML文件格式错误!");
                //不是xml文件,返回
            }
        }
原文链接:https://www.f2er.com/xml/295001.html

猜你在找的XML相关文章