12

I have a simple c# function that creates a basic XML file and saves:

private void CreateXMlFile(string Filename, string Name, string Company)
        {
            XmlDocument doc = new XmlDocument();
            XmlNode docNode = doc.CreateXmlDeclaration("1.0", "UTF-8", null);
            doc.AppendChild(docNode);

            XmlNode licenseNode = doc.CreateElement("license");
            doc.AppendChild(licenseNode);

            XmlNode node = doc.CreateElement("Name");
            node.AppendChild(doc.CreateTextNode(Name));
            licenseNode.AppendChild(node);

            node = doc.CreateElement("Company");
            node.AppendChild(doc.CreateTextNode(Company));
            licenseNode.AppendChild(node);


            doc.Save(Filename);
        }

When I try to edit or delete the file I always get following error:

The process cannot access the file because it is being used by another process.

XmlDocument doesnt have any inbuilt dispose or close routines and wondered how I can force the file to close before later editing or deleting it.

I have tried to save the file using StreamWriter:

StreamWriter outStream = System.IO.File.CreateText(outfile);
            outStream.Write(data);
            outStream.Close();

But this didnt make a difference with the same error.

Your advice is greatly accepted.

Thank you

2
  • Have you tried to write it with an instance of a stream write instead of passing the file name to save?
    – Steve
    Feb 17, 2012 at 11:01
  • I had the same problem, but my code was loading the XML first if it existed. The XmlReader object needed to be closed before the lock on the file was freed. Apr 21, 2015 at 6:59

2 Answers 2

7

Send Stream to XmlDocument's Save method instead of file name.

    private static void Main(string[] args)
    {
        CreateXMlFile("c:\\test.xml", "testName", "testCompany");
    }

    private static void CreateXMlFile(string Filename, string Name, string Company)
    {
        XmlDocument doc = new XmlDocument();
        XmlNode docNode = doc.CreateXmlDeclaration("1.0", "UTF-8", null);
        doc.AppendChild(docNode);

        XmlNode licenseNode = doc.CreateElement("license");
        doc.AppendChild(licenseNode);

        XmlNode node = doc.CreateElement("Name");
        node.AppendChild(doc.CreateTextNode(Name));
        licenseNode.AppendChild(node);

        node = doc.CreateElement("Company");
        node.AppendChild(doc.CreateTextNode(Company));
        licenseNode.AppendChild(node);
        StreamWriter outStream = System.IO.File.CreateText(Filename);

        doc.Save(outStream);
        outStream.Close();
    }

I tried executing above code and it is working fine at my end.

5
  • I tried this and when I go to delete the file I get same error.
    – Belliez
    Feb 17, 2012 at 11:43
  • Can you try changing the file location?
    – AshokD
    Feb 17, 2012 at 11:52
  • tried new location also and same. I also disabled Livedrive (online backup) in case this was interfering.
    – Belliez
    Feb 17, 2012 at 11:58
  • 1
    This problem has been dogging me for a couple days as XMLDocument.Save() seems to flush properly when in debug mode however when in Release it does not. Using a Stream solved the problem, thank you.
    – StuTheDog
    Aug 9, 2012 at 19:23
  • Have you eventually figured it out? I currently have the same problem. Nov 5, 2015 at 14:11
2

Your code is fine. I tested it on my machine and there is no lock left after Save().

Try to use Unlocker (http://www.softpedia.com/get/System/System-Miscellaneous/Unlocker.shtml) to check whether you are really the one who holds the lock.

Which .NET framework do you use? Theres also a report (http://bytes.com/topic/net/answers/467028-xmldocument-save-does-not-close-file-properly) which was not reproducable too.

2
  • Its being locked by the visual studio vshost.exe. I just compiled to .net 3.5 and same problem.
    – Belliez
    Feb 17, 2012 at 11:55
  • Could you create a blank project with that method called by a button click and check after the click whether the file is locked?
    – BlueM
    Feb 17, 2012 at 12:27

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.