简体   繁体   中英

How do I close an XMLDocument in C#

I am importing information from XML files into a SQL Database. The import is working fine but some of the XML files have errors in them so therefore the code fails. I catch the exception and try to move the file to an error folder but as I was previously reading from the file I get an error saying the file is in use by another process. I have searched but havent came across any way of curing this yet. I have tried saving the document but still is in use when I try to move, Below is a sample of the code where I try to move the file for reference. Any suggestions on how to cure this would be great and thanks in advance for the information.

XmlDocument doc = new XmlDocument();
try
{
    doc.Load(st);
}
catch(Exception ex)
{
    doc.Save(st);
    if (File.Exists(st))
        File.Move(st, st + "\\Error");
}

Try

XmlDocument doc = new XmlDocument();
try
{
    doc.Load(st);
}
catch(Exception ex)
{
    //doc.Save(st);                      -- why do you need to save a file that exists?
    //if (File.Exists(st))               -- it returns always true
    //    File.Move(st, st + "\\Error"); -- wrong path

    File.Move(st, Path.Combine("C:\\Error", Path.GetFileName(st)));
}

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