积极答复者
txt转XML的程序,转换出来的不对。请找错,谢谢

问题
-
这是网上找的程序,但运行后结果不一样,请帮忙把错误找出来,谢谢你的指点。
新建一个windows应用程序,命名该窗体为FrmTxtXml,在该窗体中添加: 一个按钮“toolStripOpen”,text为“打开文件”; 一个按钮 “toolStripConvert”,text为“转换”; 一个按钮 “ toolStripSaveas”,text为“另存为”; 一个文本框“ txtTXT”,显示txt文本; 一个文本框“txtXML”,显示xml格式文本; //**************************************** using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; using System.IO; namespace TXT转XML { //******************************************************* //运行程序,可将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> public partial class Form1 : Form { String txtContent = String.Empty; public Form1() { InitializeComponent(); } private void FrmTxtXml_Load(object sender, EventArgs e) { } //打开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); } } } }
杲大胜 ...........勤!能!补!拙!........ 勤!勤!勤!.............
答案
-
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();
执行for()语句时,lines.Length为1.跳过了for中的循环语句,为何?
杲大胜 ...........勤!能!补!拙!........ 勤!勤!勤!.............
试试
String[] lines = this.txtTXT.Text.Split(new string[] { "\r\n", "\n"}, StringSplitOptions.None);
怀疑不是回车换行结尾
- 已标记为答案 杲大盛 2013年9月5日 2:49
-
楼主,最关键部分:
public string CreateXml() { StringBuilder sbu = new StringBuilder(); //读取文本文件 string[]allLines = System.IO.File.ReadAllLines("文本文件绝对或者相对路径"); sbu.Append("<?xml version=\"1.0\" encoding=\"gbk\"?>").Append(Environment.NewLine).Append("<dataRoot>").Append(Environment.NewLine); for(int i=1;i<allLines.Length;++i) { sbu.AppendLine("<record>"); string[] values = allLines[i].Split('\t'); sbu.AppendLine("<id>"+values[0]+"</id>"); sbu.AppendLine("<name>"+values[1]+"</name>"); sbu.AppendLine("<xb>"+values[2]+"</xb>"); sbu.AppendLine("<birthday>"+values[3]+"</birthday>"); sbu.AppendLine("</record>"); } return sbu.ToString(); }
If you think one reply solves your problem, please mark it as An Answer, if you think someone's reply helps you, please mark it as a Proposed Answer
Help by clicking:
Click here to donate your rice to the poor
Click to Donate
Click to feed Dogs & Cats
Found any spamming-senders? Please report at: Spam Report- 已编辑 ThankfulHeartModerator 2013年9月2日 8:05 删除冗余重复代码
- 已标记为答案 杲大盛 2013年9月5日 2:49
全部回复
-
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();
执行for()语句时,lines.Length为1.跳过了for中的循环语句,为何?杲大胜 ...........勤!能!补!拙!........ 勤!勤!勤!.............
- 已标记为答案 杲大盛 2013年9月5日 2:48
- 取消答案标记 ThankfulHeartModerator 2013年9月5日 3:04
-
-
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();
执行for()语句时,lines.Length为1.跳过了for中的循环语句,为何?
杲大胜 ...........勤!能!补!拙!........ 勤!勤!勤!.............
试试
String[] lines = this.txtTXT.Text.Split(new string[] { "\r\n", "\n"}, StringSplitOptions.None);
怀疑不是回车换行结尾
- 已标记为答案 杲大盛 2013年9月5日 2:49
-
hello,
txt文本看来是CSV格式,可参考以下方式将文挡转成类
http://www.dotblogs.com.tw/yc421206/archive/2012/05/04/71974.aspx
http://www.dotblogs.com.tw/yc421206/archive/2012/05/04/71979.aspx然后再透过序列化将类转程xml,请参考以下
http://www.dotblogs.com.tw/yc421206/archive/2011/05/20/25595.aspx
http://www.dotblogs.com.tw/yc421206/archive/2011/07/08/31290.aspx不晓得您的XML结构如何,更进阶的使用方式请参考以下
http://www.dotblogs.com.tw/yc421206/archive/2011/12/26/63366.aspx
http://www.dotblogs.com.tw/yc421206/archive/2012/01/19/66543.aspx秘訣無它,唯勤而已 http://www.dotblogs.com.tw/yc421206/
-
楼主,最关键部分:
public string CreateXml() { StringBuilder sbu = new StringBuilder(); //读取文本文件 string[]allLines = System.IO.File.ReadAllLines("文本文件绝对或者相对路径"); sbu.Append("<?xml version=\"1.0\" encoding=\"gbk\"?>").Append(Environment.NewLine).Append("<dataRoot>").Append(Environment.NewLine); for(int i=1;i<allLines.Length;++i) { sbu.AppendLine("<record>"); string[] values = allLines[i].Split('\t'); sbu.AppendLine("<id>"+values[0]+"</id>"); sbu.AppendLine("<name>"+values[1]+"</name>"); sbu.AppendLine("<xb>"+values[2]+"</xb>"); sbu.AppendLine("<birthday>"+values[3]+"</birthday>"); sbu.AppendLine("</record>"); } return sbu.ToString(); }
If you think one reply solves your problem, please mark it as An Answer, if you think someone's reply helps you, please mark it as a Proposed Answer
Help by clicking:
Click here to donate your rice to the poor
Click to Donate
Click to feed Dogs & Cats
Found any spamming-senders? Please report at: Spam Report- 已编辑 ThankfulHeartModerator 2013年9月2日 8:05 删除冗余重复代码
- 已标记为答案 杲大盛 2013年9月5日 2:49