Convert Text.txt file .xml format
Posted
by Kasun
on Stack Overflow
See other posts from Stack Overflow
or by Kasun
Published on 2010-06-07T18:15:46Z
Indexed on
2010/06/07
18:22 UTC
Read the original article
Hit count: 418
Hi,
I try to convert text file to xml file using following code. But i get error in line 12. Could any one correct it and give me the correct answer.
private void button1_Click(object sender, EventArgs e)
{
string[] lines = File.ReadAllLines("ex3.txt");
char[] ca = new char[] { '~' };
using (XmlTextWriter writer = new XmlTextWriter("ex3.xml", null))
{
writer.Formatting = Formatting.Indented;
writer.WriteStartDocument();
writer.WriteStartElement("Root");
writer.WriteStartElement("Header");
writer.WriteStartElement("H1");
writer.WriteString(lines[0].TrimEnd().Split(ca, 2)[1]);
writer.WriteEndElement();
writer.WriteStartElement("H2");
writer.WriteString(lines[1].TrimEnd().Split(ca, 2)[1]);
writer.WriteEndElement();
writer.WriteEndElement();
writer.WriteStartElement("Details");
for (int i = 2; i < lines.Length - 2; i++)
{
writer.WriteStartElement("D" + (i - 1).ToString());
writer.WriteString(lines[i].TrimEnd().Split(ca, 2)[1]);
writer.WriteEndElement();
}
writer.WriteEndElement();
writer.WriteStartElement("Footer");
writer.WriteStartElement("F1");
writer.WriteString(lines[lines.Length - 2].TrimEnd().Split(ca, 2)[1]);
writer.WriteEndElement();
writer.WriteStartElement("F2");
writer.WriteString(lines[lines.Length - 1].TrimEnd().Split(ca, 2)[1]);
writer.WriteEndElement();
writer.WriteEndElement();
writer.WriteEndDocument();
}
}
Thanks
© Stack Overflow or respective owner