*********如何将.txt文本里的信息按照一定格式转换成.xml文件,并且保存起来!

vitti 2003-09-27 11:16:11
我有一个.txt里面有很多的东西如下:

123456789012345 1 1 4563346.00 775768.82
124434343434343 3 3 56565.00 7984656.89
344354545677878 4 45545.00 78238.89


保存成:
<Info>
<Info_mx>
<nss>123456789012345</nss>
<nsa>1</nsa>
<nsr>1</nsr>
<nsj>4563346.00</nsj>
<nsje>775768.82 </nsje>
</Info_mx>
<Info_mx>
<nss>124434343434343</nss>
<nsa>3</nsa>
<nsr>3</nsr>
<nsj>56565.00</nsj>
<nsje>7984656.89</nsje>
</Info_mx>
<Info_mx>
<nss>344354545677878</nss>
<nsa></nsa>
<nsr>4</nsr>
<nsj>45545.00</nsj>
<nsje>78238.89</nsje>
</Info_mx>
</Info>

其中有的信息可能为空,像第.txt文本中的第3行,如果数据为空(空格代替的数据)xml文件就不写这个项
...全文
275 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
cpp2017 2003-09-27
  • 打赏
  • 举报
回复
string path = Server.MapPath("aa.txt");
FileInfo aa = new FileInfo(path);
StreamReader objS=aa.OpenText();
string str=objS.ReadToEnd();
string[] ary = str.Split(new char[]{'\n'});
string res ="<Info>\n";
string[] flag = new string[]{"nss>","nsa>","nsr>","nsj>","nsje>"};
for (int i=0;i<ary.Length;i++)
{
if (ary[i].Trim().Length>0)
{
res +="<Info_mx>";

string[] ary1 = ary[i].Trim().Split(new char[]{'\t'});
for (int j=0;j<ary1.Length;j++)
{
res +="<"+ flag[j] +ary1[j].Trim() +"</"+ flag[j] ;
}
res +="</Info_mx>\n";
}
}
res +="</Info>";
res = "<xml version=\"1.0\" encoding=\"gb2312\">\n"+res +"</xml>";
FileInfo bb = new FileInfo(Server.MapPath("aa.xml"));
StreamWriter newStream = bb.CreateText();
newStream.Write(res);
newStream.Close();
newStream =null;
bb = null;
aa = null;
objS.Close();
objS = null;
Inyoureyes 2003-09-27
  • 打赏
  • 举报
回复
要为空不写那一项,可以在info数组遍历的时候加个判断就行了
Inyoureyes 2003-09-27
  • 打赏
  • 举报
回复
xml文件是这样写的,我就不写读取数据了,你先读出这些行,取到每个数据
Import System.XML

dim writer as XMLTextWriter
try
writer = new XMLTextWriter(Server.MapPath("a.xml"),nothing)
writer.Formatting = Formatting.Indented
writer.Indentation = 3
writer.WriteStartElement("Info")
while(txt文件没结束//可以用txtReader.Peek()>-1判断)
'读出txt文件的一行,假设将每个参数存在数组info中,从Info(0)开始
writer.StartElement("Info_mx")
writer.WriterElementString("nss",info(0))
writer.WriterElementString("nsa",info(1))
writer.WriterElementString("nsr",info(2))
writer.WriterElementString("nsj",info(3))
writer.WriterElementString("nsje",info(4))
writer.WriterEndElement()


end while
writer.WriterEndElement()
writer.Flush
catch e as Exception
Response.Writer("Error accessing XML file")
finally
writer.Close
Response.Writer("Finished!")
end try

我这里没有.net,所以没经过调试,提供一点思路,希望能有所帮助
流梓 2003-09-27
  • 打赏
  • 举报
回复
用IO类中函数取出.txt中的值,for循环读出.txt中的值。
这样:
for(int i=0;i<=this.listBox2.Items.Count-1;i++)
{
strItem=this.listBox2.Items[i].ToString();
for(int a=0;a<=strItem.Length-1;a++)
{
strTable+=strItem[a];
if(char.IsWhiteSpace(strItem[a]))
{
strTable="";
break;
}

}
}

以上的我是用for读出ListBox中的值:LZ 刘宗 A

试试上面的
vitti 2003-09-27
  • 打赏
  • 举报
回复
TAB键吗,是!
jackyhzzjcn 2003-09-27
  • 打赏
  • 举报
回复
中间的空格是什么? TAB键吗?

62,046

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧