Wjdavis5
Wjdavis5

Reputation: 4151

Un-Catchable IOException

ASPX.NET application that uses XDocument.Load() to read from an XML file. At times it will throw an IOException static that the file cannot be opened b/c it is in use by another process. I cannot recreate this at will by opening the file and reloading the site. But what's even more odd is that the exception occurs from within a Try-Catch block where I am explicitly catching System.IOException.

Here is the stack:

Exception type: IOException

Thread information: Thread ID: 18 Thread account name: NT AUTHORITY\NETWORK SERVICE Is impersonating: False Stack trace: at System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath) at System.IO.FileStream.Init(String path, FileMode mode, FileAccess access, Int32 rights, Boolean useRights, FileShare share, Int32 bufferSize, FileOptions options, SECURITY_ATTRIBUTES secAttrs, String msgPath, Boolean bFromProxy, Boolean useLongPath) at System.IO.FileStream..ctor(String path, FileMode mode, FileAccess access, FileShare share, Int32 bufferSize) at System.Xml.XmlDownloadManager.GetStream(Uri uri, ICredentials credentials, IWebProxy proxy, RequestCachePolicy cachePolicy) at System.Xml.XmlUrlResolver.GetEntity(Uri absoluteUri, String role, Type ofObjectToReturn) at System.Xml.XmlReaderSettings.CreateReader(String inputUri, XmlParserContext inputContext) at System.Xml.XmlReader.Create(String inputUri, XmlReaderSettings settings, XmlParserContext inputContext) at System.Xml.Linq.XDocument.Load(String uri, LoadOptions options) at System.Xml.Linq.XDocument.Load(String uri)

at StatTick.Controls.ChartSlider.getXMLFile(String url) in removedpath\StatTick\Controls\ChartSlider.ascx.cs:line 27 at StatTick.Controls.ChartSlider.Page_Load(Object sender, EventArgs e) in removedpath\StatTick\Controls\ChartSlider.ascx.cs:line 21

Here is the code:

private XDocument getXMLFile(string url)
    {
        XDocument tempDoc;

        t("Looking For XML File");
        int tryCount = 0;
        //string URL = "~/tempCharts/imageList.xml";
        while (tryCount++ < 10)
        {

            try
            {
                tempDoc = XDocument.Load(Server.MapPath(url));
                return tempDoc;
            }
            catch (IOException)
            {
                t("Error accessing XML File, sleeping for 10ms and then trying again\r\nTryCount: " + tryCount.ToString());
                Thread.Sleep(10);
                continue;

            }
        }

        return null;
    }

Hopefully someone will be able to offer some insight into this matter for me.

Thanks

EDIT Ok here is what I've Done, I'll have to do some testing to verify it no longer throws Thanks for the quick responses!:

while (tryCount <= 10)
        {
            try
            {
                using (FileStream fStream = new FileStream(Server.MapPath(url), FileMode.Open, FileAccess.Read, FileShare.Read))
                {
                    XDocument xDoc = XDocument.Load(fStream);

                    foreach (XElement xe in xDoc.Descendants("ImageUrl"))
                    {
                        t("Added: " + xe.Value);
                        tempImageUrlList.Add(xe.Value);
                    }
                    t("Done with Image List!");
                }
                return tempImageUrlList;
            }
            catch (Exception)
            {
                t("Error access XML File, sleeping for 10ms and then trying again\r\nTryCount: " + tryCount.ToString());
                Thread.Sleep(10);
                continue;
            }
        }

Upvotes: 1

Views: 546

Answers (2)

Prabhu Murthy
Prabhu Murthy

Reputation: 9261

use a stream instead and use it load the XML.You can be sure that the underlying stream is closed with a using statement.

using (Stream s = File.OpenRead(Server.MapPath(url)))
{
        XDocument.Load(s);
}

Or

using (FileStream fs= new FileStream(Server.MapPath(url), FileMode.Open,
                                          FileAccess.Read, FileShare.Read))
{
        XDocument.Load(fs);
}

;

Upvotes: 1

RVD
RVD

Reputation: 66

i think the error is becuse of the access permission for the file so please give read/write permission to the file and then try reading file.

Upvotes: 0

Related Questions