开发者

Text file to XML file convert

I going to convert text file to XML. I use the following code. But it generate error in line 12. Can anyone correct the code. Error is in this line.

writer.WriteString(lines[0].TrimEnd().Split(ca, 2)[1]); ------ "Index was outside the bounds of the array."

private void button1_Click(object sender, EventArgs e)
{
    string[] lines = File.ReadAllLines("ex3.txt");
    char[] ca = new char[] { '\n' };
    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();
        MessageBox.Show("Success");
    }


In the lines that read

writer.WriteString(lines[0].TrimEnd().Split(ca, 2)[1]);

Simply remove the .Split(ca, 2)[1]. so...

writer.WriteString(lines[0].TrimEnd());


The method File.ReadAllLines gives you an array which contains all lines in each entry.

Here you make a split on "\n" in the first line. But the line cannot contain a "\n" anymore because the ReadAllLines has already splitted the text in lines.


It means one of the arrays in that line isn't as big as you think it is - either the file ex3.txt is empty, or (far more likely) the result of splitting a single line of text (the result of a call to File.ReadAllLines() (lines[0]) on a newline character does not have a second line - which is clearly impossible.

In response to your comment:

Let's break down the line that's causing your error:

writer.WriteString(lines[0].TrimEnd().Split(ca, 2)[1]);

writer.WriteString(); does the writing...but what does it write?

lines[0] is the first line of the file ex3.txt.

lines[0].TrimEnd() is the first line of the file ex3.txt with any whitespace at the end removed.

lines[0].TrimEnd().Split(ca, 2) will return an array of at most 2 elements, consisting of the first line of the file ex3.txt with any whitespace at the end, split on the elements of the array ca, which consists of a newline. As the string contains one line, it will never have more than 1 element.

lines[0].TrimEnd().Split(ca, 2)[1] is therefore going to cause an exception every time, because you try and access the second element of an array that will never have more than one element.

It is impossible to post code that will solve your problem without more details of the problem you're trying to solve, and the exact format of the input file.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜