简体   繁体   中英

Trying to Load an XML File Upon Form Load, Getting Error

In C# am trying to check to see if an XML file is created, if not create the file and then create the xml declaration, a comment and a parent node.

When I try to load it, it gives me this error:

"The process cannot access the file 'C:\\FileMoveResults\\Applications.xml' because it is being used by another process."

I checked the task manager to ensure it wasn't open and sure enough there were no open applications of it. Any ideas of what's going on?

Here is the code I am using:

//check for the xml file
if (!File.Exists(GlobalVars.strXMLPath))
{
//create the xml file 
File.Create(GlobalVars.strXMLPath);

//create the structure
XmlDocument doc = new XmlDocument();
doc.Load(GlobalVars.strXMLPath);

//create the xml declaration 
XmlDeclaration xdec = doc.CreateXmlDeclaration("1.0", null, null);

//create the comment 
XmlComment xcom = doc.CreateComment("This file contains all the apps, versions, source and destination paths.");

//create the application parent node
XmlNode newApp = doc.CreateElement("applications");

//save
doc.Save(GlobalVars.strXMLPath); 

Here is the code I ended up using to fix this issue: //check for the xml file if (!File.Exists(GlobalVars.strXMLPath))
{
using (XmlWriter xWriter = XmlWriter.Create(GlobalVars.strXMLPath)) { xWriter.WriteStartDocument(); xWriter.WriteComment("This file contains all the apps, versions, source and destination paths."); xWriter.WriteStartElement("application"); xWriter.WriteFullEndElement(); xWriter.WriteEndDocument(); }

File.Create() returns a FileStream that locks the file until it's closed.

You don't need to call File.Create() at all; doc.Save() will create or overwrite the file.

I would suggest something like this:

string filePath = "C:/myFilePath";
XmlDocument doc = new XmlDocument();
if (System.IO.File.Exists(filePath))
{
    doc.Load(filePath);
}
else
{
    using (XmlWriter xWriter = XmlWriter.Create(filePath))
    {
        xWriter.WriteStartDocument();
        xWriter.WriteStartElement("Element Name");
        xWriter.WriteEndElement();
        xWriter.WriteEndDocument();
    }

    //OR

    XmlDeclaration xdec = doc.CreateXmlDeclaration("1.0", null, null);
    XmlComment xcom = doc.CreateComment("This file contains all the apps, versions, source and destination paths.");
    XmlNode newApp = doc.CreateElement("applications");
    XmlNode newApp = doc.CreateElement("applications1");
    XmlNode newApp = doc.CreateElement("applications2"); 
    doc.Save(filePath); //save a copy
}

The reason your code is currently having problems is because of: File.Create creates the file and opens the stream to the file, and then you never make use of it (never close it) on this line:

//create the xml file 
File.Create(GlobalVars.strXMLPath);

if you did something like

//create the xml file 
using(Stream fStream = File.Create(GlobalVars.strXMLPath)) { }

Then you would not get that in use exception.

As a side note XmlDocument.Load will not create a file, only work with an already create one

You could create a stream, setting the FileMode to FileMode.Create and then use the stream to save the Xml to the path specified.

using (System.IO.Stream stream = new System.IO.FileStream(GlobalVars.strXMLPath, FileMode.Create))
{
    XmlDocument doc = new XmlDocument();
    ...
    doc.Save(stream);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM