1

I'm having trouble saving my XML file after I have called load. This function is called twice - once when "toSave" is set to false, and then the second time is when it is set to true. On the second time round, the save causes an exception. I tried adding a Dispose and Close call to the XMLReader but nothing seems to help. Here is the code:

// Check if the file exists
if (File.Exists(filePath))
{
    try
    {
        // Load the file
        XmlReaderSettings readerSettings = new XmlReaderSettings();
        readerSettings.IgnoreComments = true;
        XmlReader reader = XmlReader.Create(filePath, readerSettings);

        XmlDocument file = new XmlDocument();
        file.Load(reader);

        XmlNodeType type;
        type = file.NodeType;
        if (toSave)
            ModifyXMLContents(file.FirstChild.NextSibling, null);
        else
            PopulateNode(file.FirstChild.NextSibling, null);

        // Save if we need to
        if (toSave)
            file.Save(filePath);

        reader.Dispose();
        reader.Close();
    }
    catch (Exception ex)
    {
        // exception is: "The process cannot access the file d:\tmp\10.51.15.2\Manifest.xml" because it is being used by another process
        Console.WriteLine(ex.Message);
    }
}

Any help would be greatly appreciated.

4
  • 4
    Please do not post images of code. Copy and paste into the question - not all users can access the image, or they may be on mobile devices and not able to see the image clearly. Commented Jan 13, 2016 at 17:22
  • I would suggest looking at this article in regards to how to effectively create XML document Other XML Technologies Commented Jan 13, 2016 at 17:26
  • You try to save the file before you close the reader. Most likely it's the reader that has the lock on the file. Try closing the reader before calling Save (and consider using a using block as well). Commented Jan 13, 2016 at 17:27
  • Sorry about that Tim. I usually post the code but thought it was easier this way. Cheers Commented Jan 13, 2016 at 20:12

2 Answers 2

4

The XmlReader you created is still open when you try to save it, and hence it is locking the file and preventing the save.

Once loaded into the XmlDocument, you don't need the reader anymore, so you can close/dispose it before you attempt the save and then the save should work.

For example:

XmlReaderSettings readerSettings = new XmlReaderSettings();
readerSettings.IgnoreComments = true;            

XmlDocument file = new XmlDocument();

using (XmlReader reader = XmlReader.Create(filePath, readerSettings))            
    file.Load(reader);

/* do work with xml document */

if (save)
    file.Save(filePath);
Sign up to request clarification or add additional context in comments.

Comments

0

For me this was caused by the file being written out into a directory with MANY (100k) other files. Once I cleared out all the other files the problem went away. NTFS appears to get cranky with very large numbers of files in a single folder.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.