Reading the end of an XML file

I have a program that cycles through an XML file and outputs to another file every 1000 records. Thyis works fine until the end of the XML file is reached, when it fails saying my 'root' tag is not closed - although it does for the previous files. It seems to be having issues with recognising the end of the XML file, so can someone please confirm how this is done Currently I have a while loop to do this, with a for loop inside to cycle through the records, ie:

while (XmlReader.Read())
{
...

for (int i = 1; i <= 100; i++)
{
while (reads in a node here)
{
does the work
}
}

Close the XmlReader and XmlWriter
}

This works fine for the beginning, until the end of the file is reached; is my way of reading the XML file correct (I have also tried XmlReader != null), or is there a better way

Many thanks.


Answer this question

Reading the end of an XML file

  • Kitty Butler

    Neither idea makes any difference - the same error (about the closing '>') occurs...
  • Roy mm

    pls send me the error message exact one

  • Chardiot

    wat u r doing seems fine

    read() takes care of it.

    u could also use while (!XmlReader.Eof())



  • Mark See

    I've tried that before without any joy. It doesn't seem to make any difference, as the same error occurs.

    I will try Alex's suggestion too, but I suspect I will get the same problem...


  • CodeJingle

    Of course.

    The program itself runs but doesn't complete - so there isn't actually an error here. If I lower the for loop's value (so the end of file isn't reached), it runs to completion and works fine, although obviously enough records aren't read.

    The XML works fine for each file created, except for the last one if the end of file is reached - then I get the error mentioned in my last post.

    My code is as follows:

    while (XmlReader.Read() && nodeCounter <= 1000)

    {

    // Create a file to write to

    XmlTextWriter XmlWriter = new XmlTextWriter(directory + "\\Output" + x + " CIS_KASN.xml", null);

    XmlWriter.Formatting = Formatting.Indented;

    // Write the header details to the new file

    XmlWriter.WriteProcessingInstruction("xml", "version='1.0' encoding='ISO-8859-1'");

    XmlWriter.WriteComment("This XML message contains CIS Key Accounts and Special Needs records for inserting/updating in the IAR");

    XmlWriter.WriteStartElement("message");

    XmlWriter.WriteAttributeString("xmlns", "xsi", null, "http://www.w3.org/2001/XMLSchema-instance");

    XmlWriter.WriteAttributeString("xsi", "noNamespaceSchemaLocation", null, "CISKASN.xsd");

    // Write each row to the file, until 1000 records have been written

    for (int i = 1; i <= 1000; i++)

    {

    while (XmlReader.Name != "CISKASNRecord")

    {

    XmlReader.Read();

    }

    XmlWriter.WriteNode(XmlReader, true);

    XmlWriter.Flush();

    }

    // Tidy up and close the file so that a new one can be opened

    XmlWriter.Flush();

    XmlWriter.Close();

    nodeCounter++;

    x++;

    }


  • newbieneedshelp

    Maybe it's not that, but the problem is still there. It seems to definitely have a problem with reaching the end of the file, if it doesn't the program is fine, if it does it continues to run and I just get an XML error. I just can't see what is wrong with my code...
  • bitbonk

    Now the program runs without stopping again and I get 2 XML files, both with errors this time. The first one has

    "Invalid at the top level of the document."

    At the bottom and the second has

    "XML document must have a top level element."


  • Sai A

    I tried that...still no joy, but I get a different XML error. This time it's "End element was missing the character '>'". Hopefully this is something obvious, although I don't understand how it finds the end element without the closing '>', if that is what is happening!

    Trying Alex's suggestion didn't work either - the same problem as before...


  • fya

    try writeString(">")

    before flushing.

    or

    writer.WriteFullEndElement(); instead of writeendelement()



  • rb531

    can u pls send me the exact error..

    and the xml if pssible



  • Jeffrey Brown

    hi got it ..

    include this after the for..loop

    bfore the //tidy up comment

    //Write the close tag for the root element.
    xmlwriter.WriteEndElement();



  • kiran1234

    also remove the flush from inside the for loop..

    along with my changes.. it will work man..it shud



  • Gordonn

    Very sorry, I meant to add that to the end of my post!

    The error is:

    "XML page cannot be displayed.

    The following tags were not closed: message. Error processing resource 'file:///C:/..."


  • Joo Park

    I do not know if this will help but I seemed to have a problem of similar nature recently but not with the end of an XML file--with the beginnig gof it. I used a different class to read it and I found out that it read one file but not the other. On close examination I could not discover any difference in the space of about 20 lines: the schema, etc. So I just copied the schema from the "good" file over to the "bad" one and now everything works fine.

    Find a similar file somewhere with a comparable schema and read it, see how it does.

    Perhaps it will help, perhaps not.



  • Reading the end of an XML file