Mate282
Mate282

Reputation: 23

xmlDocument is already used by another process

I've created a program that read and write values from an XML file. When I try to save the XML with xmlDoc.Save(path); it throws an exception because the file is already used by another process.

Read Method:

private void ReadXml()
        {
            //instantiate the xmlDocument
            xmlDoc = new XmlDocument();
            //declare an XmlNodeList
            XmlNodeList xmlNode;

            //create a FileStream to read the file
            FileStream fs = new FileStream(path, FileMode.Open, FileAccess.Read);
            //load the file
            xmlDoc.Load(fs);
            //get all the nodes from the file
            xmlNode = xmlDoc.GetElementsByTagName("Drop");

            //read all values frome the nodes and save it into variables
            for (int i = 0; i <= xmlNode.Count - 1; i++)
            {
                string name= xmlNode[i].ChildNodes.Item(0).InnerText.Trim();
                int DefaultMin= Convert.ToInt32(xmlNode[i].ChildNodes.Item(1).InnerText.Trim());
                int DefaultMax = Convert.ToInt32(xmlNode[i].ChildNodes.Item(2).InnerText.Trim());
                int min = Convert.ToInt32(xmlNode[i].ChildNodes.Item(3).InnerText.Trim());
                int max= Convert.ToInt32(xmlNode[i].ChildNodes.Item(4).InnerText.Trim());
                int line= Convert.ToInt32(xmlNode[i].ChildNodes.Item(5).InnerText.Trim());
                string lineToChange = xmlNode[i].ChildNodes.Item(6).InnerText;
                string filePath = xmlNode[i].ChildNodes.Item(7).InnerText.Trim();

                //create the DropItem object
                DropItem drop = new DropItem(name, DefaultMin, DefaultMax, line, lineToChange, installPath+filePath);
                drop.MinValue = min;
                drop.MaxValue = max;

                //add the object to the list
                drops.Add(drop);
            }
        }

Write Method:

public void WriteXml()
        {
            //declare a xmlNodeList
            XmlNodeList xmlNode;
            //create a FileStream to read the file
            FileStream fs = new FileStream(path, FileMode.Open, FileAccess.Read);
            //load the file
            xmlDoc.Load(fs);
            //get all the nodes from the file
            xmlNode = xmlDoc.GetElementsByTagName("Drop");

            //write the values in the xml
            for (int i = 0; i <= drops.Count - 1; i++)
            {            
                xmlNode[i].ChildNodes.Item(3).InnerText=drops[i].MinValue.ToString();
                xmlNode[i].ChildNodes.Item(4).InnerText = drops[i].MaxValue.ToString();   
            }

            //save the document
            xmlDoc.Save(path);
        }

I think I have to close the xmlDoc in the read method before saving it.

Upvotes: 0

Views: 253

Answers (3)

Fran Cerezo
Fran Cerezo

Reputation: 948

Try to Dispose the FileStream object at the end of ReadXml and WriteXml methods.

Upvotes: 0

traveler3468
traveler3468

Reputation: 1656

Try adding using to the File Stream this should help guarantee that the file from the File Stream is not in use.

public void WriteXml()
{
    //declare a xmlNodeList
    XmlNodeList xmlNode;

    //create a FileStream to read the file
    using (FileStream fs = new FileStream(path, FileMode.Open, FileAccess.Read))
    {
        //load the file
        xmlDoc.Load(fs);

        //get all the nodes from the file
        xmlNode = xmlDoc.GetElementsByTagName("Drop");

        //write the values in the xml
        for (int i = 0; i <= drops.Count - 1; i++)
        {            
            xmlNode[i].ChildNodes.Item(3).InnerText=drops[i].MinValue.ToString();
            xmlNode[i].ChildNodes.Item(4).InnerText = drops[i].MaxValue.ToString();   
        }
    }

    //save the document
    xmlDoc.Save(path);
}

Upvotes: 0

Jonathan Applebaum
Jonathan Applebaum

Reputation: 5986

You are not closing the stream,
use Using statement in order to dispose and close the FileStream :

using (FileStream fs = new FileStream(path, FileMode.Open, FileAccess.Read))
{
    //load the file
    xmlDoc.Load(fs);
    //get all the nodes from the file
    xmlNode = xmlDoc.GetElementsByTagName("Drop");
}

Upvotes: 1

Related Questions