none
How to close the XmlDocument after loading from disk correctly? RRS feed

  • Question

  • Dear All,

    How to close the XmlDocument after loading from disk correctly?

    If we read the XML data file from disk by using doc.Load(reader);

    What is the correct way to close the file?

    The error occurs when calling doc.Save("books.xml");

    Where is wrong?

    Thanks and Best regards,

    E-John

            static void Main(string[] args)
            {
                //Create the XmlDocument.
                XmlDocument doc = new XmlDocument();

                //Load the the document with the last book node.
                XmlTextReader reader = new XmlTextReader("books.xml");
                reader.WhitespaceHandling = WhitespaceHandling.None;
                reader.MoveToContent();
                reader.Read();
                reader.Skip(); //Skip the first book.
                reader.Skip(); //Skip the second book.
                doc.Load(reader);

                doc.Save("books.xml");

                Console.ReadLine();
            }


    • Edited by E-John Friday, July 13, 2018 2:06 AM
    Friday, July 13, 2018 2:06 AM

Answers

  • Check this:

    using( XmlTextReader reader = new XmlTextReader( "books.xml" ) )

    {

       . . .

    }

     

    doc.Save( "books.xml" );


    • Marked as answer by E-John Tuesday, July 17, 2018 1:07 AM
    Friday, July 13, 2018 5:56 AM
  • Hi E-John,

    As far as I know, the XmlDocument.Load(filename) method does *not* leave the file open. In fact, there's no XmlDocument method for Closing the file, so it obviously takes care of all that behind-the-scenes stuff for you.


    ~~Bonnie DeWitt [C# MVP]

    http://geek-goddess-bonnie.blogspot.com

    • Marked as answer by E-John Tuesday, July 17, 2018 1:07 AM
    Monday, July 16, 2018 3:37 PM
    Moderator
  • Looking at your Main method I cannot see any changes made to your XmlDocument. Of course, you don't need to save your 'doc' object back to file, if there aren't any modifications you need to save! I think, 'doc.Save(...)' only makes sense when you are going to actually modify your XmlDocument.

    If you really want to modify or edit your file, you should thing about using the 'using' statement as shown by Viorel in his post, because this statement takes also care for closing and finalizing the stream if necessary. And you might take a look at this note by Microsoft:

    Starting with the .NET Framework 2.0, we recommend that you create XmlReader instances by using the XmlReader.Create method to take advantage of new functionality.

    wizend


    • Edited by Wizend Monday, July 16, 2018 8:59 PM explanation
    • Marked as answer by E-John Tuesday, July 17, 2018 1:08 AM
    Monday, July 16, 2018 8:56 PM

All replies

  • Check this:

    using( XmlTextReader reader = new XmlTextReader( "books.xml" ) )

    {

       . . .

    }

     

    doc.Save( "books.xml" );


    • Marked as answer by E-John Tuesday, July 17, 2018 1:07 AM
    Friday, July 13, 2018 5:56 AM
  • Hi Viorel_,

    Thanks for your reply.

    Is it safe if we just call doc.Load() but don't call doc.Save() when exiting function call?

    Thanks and Best regards,

    E-John

    Friday, July 13, 2018 6:49 AM
  • Hi E-John,

    What do you mean by "is it safe"?


    ~~Bonnie DeWitt [C# MVP]

    http://geek-goddess-bonnie.blogspot.com

    Sunday, July 15, 2018 10:19 PM
    Moderator
  • The "using" statement merely ensures that the XmlTextReader will be closed when you leave the statements it protects.  That's true whether you leave it by flowing out the bottom or returning early.

    Tim Roberts | Driver MVP Emeritus | Providenza & Boekelheide, Inc.

    Monday, July 16, 2018 2:36 AM
  • Hi BonnieB,

    Thanks for your reply, here is what I mean "is it safe",

    If we read data from XML file by doc.Load(), but this function does not close,

    In C language, we need to close the file when exit the function, 

    I know that we can use doc.Save() to close it, but if this function is called, save operation will be executed,

    I just want to open the file like C language did and close the file like C language did which save operation is not involved.

    Thanks and Best regards,

    E-John

               

            public static int GetBookNumber(string fileName)
            {
                int bookNumber = 0;

                XmlDocument doc = new XmlDocument();

                doc.Load(fileName);

               // Only load data from file, but does't close this file at the end of this funciton           //  Is that "safe"? CLR will close it automatically?

                foreach (XmlNode x in doc.SelectSingleNode("/bookstore"))
                {
                    bookNumber++;
                }

                return bookNumber;
            }

    Monday, July 16, 2018 7:14 AM
  • Hi E-John,

    As far as I know, the XmlDocument.Load(filename) method does *not* leave the file open. In fact, there's no XmlDocument method for Closing the file, so it obviously takes care of all that behind-the-scenes stuff for you.


    ~~Bonnie DeWitt [C# MVP]

    http://geek-goddess-bonnie.blogspot.com

    • Marked as answer by E-John Tuesday, July 17, 2018 1:07 AM
    Monday, July 16, 2018 3:37 PM
    Moderator
  • Looking at your Main method I cannot see any changes made to your XmlDocument. Of course, you don't need to save your 'doc' object back to file, if there aren't any modifications you need to save! I think, 'doc.Save(...)' only makes sense when you are going to actually modify your XmlDocument.

    If you really want to modify or edit your file, you should thing about using the 'using' statement as shown by Viorel in his post, because this statement takes also care for closing and finalizing the stream if necessary. And you might take a look at this note by Microsoft:

    Starting with the .NET Framework 2.0, we recommend that you create XmlReader instances by using the XmlReader.Create method to take advantage of new functionality.

    wizend


    • Edited by Wizend Monday, July 16, 2018 8:59 PM explanation
    • Marked as answer by E-John Tuesday, July 17, 2018 1:08 AM
    Monday, July 16, 2018 8:56 PM
  • Dear All,

    In summary, there are two cases,

    1. If there is any modification in XML file, using the 'using' statement which take care for closing and finalizing the stream safely.

    it is like below

    using( XmlTextReader reader = new XmlTextReader( "books.xml" ) )
    
    {
    
    // do some modifications in this block 
    
      . . .
    
    }
    
     doc.Save( "books.xml" );
    // use doc.Save() to store the modifications

    2. If there is no modification in XML file, just read data for check, using XmlDocument.Load(filename), 

        The CLR or C# environment takes care of all that behind-the-scenes stuff.

        From Bonnie's reply "the XmlDocument.Load(filename) method does *not* leave the file open. In fact, there's no XmlDocument method for Closing the file, so it obviously takes care of all that behind-the-scenes stuff for you."

    It is like below,

            public static int GetBookNumber(string fileName)
            {
                int bookNumber = 0;
    
                XmlDocument doc = new XmlDocument();
    
                doc.Load(fileName);
    
               // There is no modification after doc.Loda(fileName) and Do some check or calculation in this block
    
                foreach (XmlNode x in doc.SelectSingleNode("/bookstore"))
                {
                    bookNumber++;
                }
    
                return bookNumber;
            }


    Thanks and Best regards,

    E-John





    • Edited by E-John Tuesday, July 17, 2018 1:12 AM
    Tuesday, July 17, 2018 1:05 AM