现在很多软件都是以xml文件作为数据源,而很多数据工具如pb等却只能另存为txt、excel等格式,为此需要一工具能将txt文本转换成xml文件。google了一下,没找到合适的,冲动之下用C#写了一个txt文本转xml格式文本的小程序,代码如下。
新建一个windows应用程序,命名该窗体为FrmTxtXml,在该窗体中添加:
一个按钮” toolStripOpen”,text为“打开文件”;
一个按钮” toolStripConvert”,text为“转换”;
一个按钮” toolStripSaveas”,text为“另存为”;
一个文本框” txtTXT”,显示txt文本;
一个文本框”txtXML5”,显示xml格式文本;
|
public partial class FrmTxtXml : Form { String txtContent = String.Empty; // public FrmTxtXml() { InitializeComponent(); }
//打开txt文件 private void toolStripOpen_Click(object sender, EventArgs e) { using (OpenFileDialog fileDialog = new OpenFileDialog()) { fileDialog.Filter = "文本文件(*.txt)|*.txt"; if (fileDialog.ShowDialog() == DialogResult.OK) { String fileName = fileDialog.FileName; if (!String.IsNullOrEmpty(fileName)) { using (StreamReader st = new StreamReader(fileName, System.Text.Encoding.GetEncoding("GBK"))) { txtContent = st.ReadToEnd(); //读取txt文件到txtTXT文本框 this.txtTXT.Text = txtContent; st.Close(); } } } } }
//将txt文件内容转换成xml格式内容 private void toolStripConvert_Click(object sender, EventArgs e) { try { //将txt内容分解为行数组 String[] lines = this.txtTXT.Text.Split(new string[] { "\r\n" }, StringSplitOptions.None); String[] heads = null; if (lines != null && lines.Length > 0) { //读取第一行数据,该行数据为xml文件的节点描述数据 heads = lines[0].Split(new string[] { "\t" }, StringSplitOptions.None); //MessageBox.Show(heads.Length.ToString() + " " + heads[0]); } // StringBuilder sb = new StringBuilder(); sb.Append("<?xml version=\"1.0\" encoding=\"gbk\"?>").Append(Environment.NewLine).Append("<dataRoot>").Append(Environment.NewLine); //生成xml节点 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[] { "\t" }, StringSplitOptions.None); sb.Append(createNode(heads, info)); } sb.Append("</dataRoot>"); this.txtXML.Text = sb.ToString(); } catch (Exception exp) { MessageBox.Show(exp.Message); } }
//产生xml节点 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(); }
//将txtXML文本框内容另存为xml文件 private void toolStripSaveas_Click(object sender, EventArgs e) { try { String fileName = ""; using (SaveFileDialog fileDialog = new SaveFileDialog()) { fileDialog.Filter = "XML数据文件(*.xml)|*.xml"; if (fileDialog.ShowDialog() == DialogResult.OK) { fileName = fileDialog.FileName; if (!String.IsNullOrEmpty(fileName)) { FileStream fs = new FileStream(fileName, FileMode.Create); //获得字节数组 byte[] data = System.Text.Encoding.GetEncoding("GBK").GetBytes(this.txtXML.Text); //开始写入 fs.Write(data, 0, data.Length); //清空缓冲区、关闭流 fs.Flush(); fs.Close(); } } } MessageBox.Show(String.Format("文件成功保存到{0}", fileName)); } catch (Exception exp) { MessageBox.Show(exp.Message); } } }
|
运行程序,可将txt文本转换为下述所示xml内容。
|
emp_no emp_name emp_xb emp_bir 11865 张三 1 1984/11/24 00:00:00 10 张三李 0 1963/7/16 00:00:00 100 张四李 0 1958/1/13 00:00:00 1000 张五李 1 1976/1/12 00:00:00 100001 张六李 0 1977/1/12 00:00:00 100002 张七李 1 1978/1/12 00:00:00 <?xml version="1.0" encoding="gbk"?> <dataRoot> <record> <emp_no> 11865</emp_no> <emp_name>张三</emp_name> <emp_xb>1</emp_xb> <emp_bir>1984/11/24 00:00:00</emp_bir> </record> <record> <emp_no>10</emp_no> <emp_name>张三李</emp_name> <emp_xb>0</emp_xb> <emp_bir>1963/7/16 00:00:00</emp_bir> </record> <record> <emp_no>100</emp_no> <emp_name>张四李</emp_name> <emp_xb>0</emp_xb> <emp_bir>1958/1/13 00:00:00</emp_bir> </record> <record> <emp_no>1000</emp_no> <emp_name>张五李</emp_name> <emp_xb>1</emp_xb> <emp_bir>1976/1/12 00:00:00</emp_bir> </record> <record> <emp_no>100001</emp_no> <emp_name>张六李</emp_name> <emp_xb>0</emp_xb> <emp_bir>1977/1/12 00:00:00</emp_bir> </record> <record> <emp_no>100002</emp_no> <emp_name>张七李</emp_name> <emp_xb>1</emp_xb> <emp_bir>1978/1/12 00:00:00</emp_bir> </record> </dataRoot> |
介绍了一款使用C#编写的TXT文本转换为XML格式的小程序,包括实现原理与具体步骤,并展示了转换前后的内容对比。
579

被折叠的 条评论
为什么被折叠?



