txt数据⽂件转换为xml数据⽂件
最近在写⼀些查询之类的⼩app,发现很多数据都是⽤excel,txt,或者数据库⽂件储存的,⽽⽤xml⽂件的⽐较少,所以我想⽤程序来实现数据间的转换。
在这⾥我写的主要是txt⽂件转换为xml⽂件,如果你的数据是在excel⾥⾯存属的,你可以通过将excel⽂件数据导出-〉⽣成txt⽂件,然后⽤下⾯的转换⼯具转换⼀下。
我写的是WPF程序,是参考园内⼀个作者的程序写的,谢谢了先,(sorry,我忘记是谁了)。
现在主要介绍⼀下程序的基本流程:
1,读取txt⽂件,我们⽤excel直接导出的⽂件的每⼀⾏的格式是⼀样的,所以很好⽤来解析⾥⾯的数据。源程序为:
private void open_Click(object sender, RoutedEventArgs e)
{
OpenFileDialog filedialog = new OpenFileDialog();
filedialog.Filter = "⽂本⽂件(*.txt)|*.txt";
if(filedialog.ShowDialog() == true)
{
string filename = filedialog.FileName;
if(!string.IsNullOrEmpty(filename))
{
StreamReader sr = new StreamReader(filename,System.Text.Encoding.GetEncoding("GBK"));
txtcontent = sr.ReadToEnd();
}
}
}
View Code
2,读取txt⽂件之后保存到string⾥⾯,然后开始转换,转换有两种,⼀种是都转换成节点的那种,第⼆种是转换成属性的那种,源代码为:
转换之前我们需要create source node, 代码如下:
private string createNode(string[] head,string[] info)
{
StringBuilder node = new StringBuilder();
node.Append("<record>").Append(Environment.NewLine);
for (int i = 0; i < head.Length; i++)
{
node.Append("<" + head[i] + ">" + "<" + info[i] + ">" + "</" + head[i] + ">").Append(Environment.NewLine);
}
node.Append("</record>").Append(Environment.NewLine);
return node.ToString();
}
Create node
现在我们有了节点了,就可以转换我们的txt⽂件了,下⾯是第⼀种⽅案:
private void convert_Click(object sender, RoutedEventArgs e)
{
string[] lines = txtcontent.Split(new string[] {"\r\n"}, StringSplitOptions.None);
string[] heads = null;
if (lines != null || lines.Length > 0)
{
heads = lines[0].Split(new string[] {"\t"},StringSplitOptions.None);
}
StringBuilder sb = new StringBuilder();
sb.Append("<?xml version=\"1.0\" encoding=\"gbk\"?>").Append(Environment.NewLine).Append("<dataRoot>").Append(Environment.NewLine);
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>");
txtxml = sb.ToString();
}
Convert to xml node
接着是第⼆个⽅案,就是转换为属性的那种形式,这是在我使⽤中需要这样快捷的格式,没有采⽤上⾯的那个create node的那个function,写的有点乱,
private void convert2_Click(object sender, RoutedEventArgs e)
{
string[] lines = txtcontent.Split(new string[] { "\r\n" }, StringSplitOptions.None);
string[] heads = null;
if (lines != null || lines.Length > 0)
{
heads = lines[0].Split(new string[] { "\t" }, StringSplitOptions.None);
}
StringBuilder sb = new StringBuilder();
sb.Append("<?xml version=\"1.0\" ?>").Append(Environment.NewLine).Append("<dataRoot>").Append(Environment.NewLine);
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("<record " + heads[0] + "=\"" + info[0] + "\" " + heads[1] + "=\"" + info[1] + "\" " + heads[2] + "=\"" + info[2] + "\" " + heads[3] + "=\"" + info[3] + "\" " + heads[4] + "=\"" + info[4] + "\" " +heads[5] + "=\"" + info[5 }
sb.Append("</dataRoot>");
txtxml = sb.ToString();
}
Convert to xml with attribute
3,到现在转换已经完成,然后就开始存属这个xml⽂件了,存储的代码如下:
private void saveas_Click(object sender, RoutedEventArgs e)
{
try
{
string filename = string.Empty;
SaveFileDialog save = new SaveFileDialog();
save.Filter = "xml⽂件|*.xml";
if (save.ShowDialog() == true)
{
filename = save.FileName;
if (!string.IsNullOrEmpty(filename))
{
FileStream fs = new FileStream(filename, FileMode.Create);
byte[] data = System.Text.Encoding.GetEncoding("GBK").GetBytes(txtxml); fs.Write(data, 0, data.Length);
fs.Flush();
fs.Close();
}
}
游戏xml文件修改MessageBox.Show("ok, gdo, then path is {0}", filename);
}
catch
{
return;
}
}
Save xml
到现在我们算是将txt⽂件转换成xml⽂件了,
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论