Convert Text.txt file .xml format
- by Kasun
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