0

I have a web service client that pull some information successfully. It's a Windows form application and the output is pulled in a textBox, which is correct.

Here you have part of my code:

if (GetData != null && GetData.Length > 0)
{
    textBox1.Text += "Data Type" + ", ";
    textBox1.Text += "\r\n";

        for (int s = 0; s < GetData.Length; ++s)
        {
            textBox1.Text += GetData[s].ID + ", ";                            
            textBox1.Text += "\r\n";
        }
}

I want to output this into a XML file. To achieve this I have include the follow code. The files gets created successfully but it only include some data. I am suppose to get over 100 lines and I only get 4 lines at the same time. When I refresh the XML created file, the data change but It show only 4 lines and not all the 100 lines. What I can be missing?

Thanks

using (XmlTextWriter writer = new XmlTextWriter("Data.xml", null))
{
    writer.WriteStartDocument();
    writer.Formatting = Formatting.Indented;
    writer.WriteStartElement("GetData");
    writer.Formatting = Formatting.Indented;

    for (int r = 0; r < GetData.Length; ++r)
    {
        writer.Formatting = Formatting.Indented;
        writer.WriteStartElement("DataDetail");                                   
        writer.WriteElementString("DataType", GetData[r].ID);
        writer.WriteEndElement();                               
    }
    writer.WriteEndElement();
    writer.WriteEndDocument();
} 
A arancibia
  • 281
  • 4
  • 5
  • 19
  • Please show a short but complete program demonstrating the problem. (You shouldn't need any UI code to do so - just a console app would make it much easier to help you.) Any reason you don't want to just create an `XDocument` and use `XDocument.Save` though? (The code you've got should work, but it's typically much simpler to build the whole document in memory.) – Jon Skeet Jun 03 '15 at 19:32
  • Hi, The problem is that the XML file only keeps few lines every time the data get written into the textBox1. when the execution ends, the textBox1 shows 100 lines, and the XML file only show the last 4 lines over the 100 lines from the textBox1. – A arancibia Jun 03 '15 at 21:40
  • You're *appending* to the textbox, but *overwriting* the file. I suspect that's the problem. – Jon Skeet Jun 03 '15 at 21:43
  • yes I think that can be the problem, but there's a way to append the XML file? – A arancibia Jun 04 '15 at 14:53
  • Well you could read the whole thing in, add the elements, then write it out again. It would be more sensible to collect *everything* you want to write though, and then just write it once. – Jon Skeet Jun 04 '15 at 15:18

0 Answers0