Q:

How can I find out why my XML file is already in use?

when I write the XML file to hard drive using the following code
    XmlDocument doc = new XmlDocument();
    doc.Load("D:\\project\\data.xml");
    if (!Directory.Exists("D:\\project_elysian\\data\\" + System.DateTime.Today.ToString("dd-MM-yyyy")))
    {
        DirectoryInfo di = Directory.CreateDirectory("D:\\project_elysian\\data\\" + System.DateTime.Today.ToString("dd-MM-yyyy"));
    }
    XmlTextWriter writer = new XmlTextWriter("D:\\project_elysian\\data\\" + System.DateTime.Today.ToString("dd-MM-yyyy") + "\\" + System.DateTime.Now.ToString("HH-mm-ss") + ".xml", null);
    XmlTextWriter writerlatest = new XmlTextWriter("D:\\project\\data\\latest\\today.xml", null);
    writer.Formatting = Formatting.Indented;
    writerlatest.Formatting = Formatting.Indented;
    doc.Save(writer);
    doc.Save(writerlatest);
    doc = null;
    writer.Flush();
    writerlatest.Flush();

it writes the XML file as desired but after that when I try to read that XML file in the same asp.net page (code placed in a C# Code Behind file) using the following code, it gives an error
    string filename = "D:\\project\\data\\latest\\today.xml";

    XmlSerializer serializer = new XmlSerializer(typeof(searchResult));
    serializer.UnknownNode += new XmlNodeEventHandler(serializer_UnknownNode);
    serializer.UnknownAttribute += new XmlAttributeEventHandler(serializer_UnknownAttribute);
    FileStream fs = new FileStream(filename, FileMode.Open);

the error is as follows
The process cannot access the file 'D:\project\data\latest\today.xml' because it is being used by another process.

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. 

Exception Details: System.IO.IOException: The process cannot access the file 'D:\project\data\latest\today.xml' because it is being used by another process.

EDIT: The file isn't being used by any other process

A:

Make sure you're closing your writer with a call like writer.Close();

