Timeout error when loading Xml from URL

asp.net, c#, xml

Solution

Don't use the Load method of the XmlDataDocument class directly; you have little to no way of influencing the behaviour when it comes to long running HTTP requests.

Instead, use the HttpWebRequest and HttpWebResponse classes to do the work for you, and then load the subsequent response into your document.

For example:

    HttpWebRequest rq = WebRequest.Create("http://www.globalgear.com.au/productfeed.xml") as HttpWebRequest;
    //60 Second Timeout
    rq.Timeout = 60000;
    //Also note you can set the Proxy property here if required; sometimes it is, especially if you are behind a firewall - rq.Proxy = new WebProxy("proxy_address");
    HttpWebResponse response = rq.GetResponse() as HttpWebResponse;


    XmlTextReader reader = new XmlTextReader(response.GetResponseStream());

    XmlDocument doc = new XmlDocument();
    doc.Load(reader);

I've tested this code in a local app instance and the XmlDocument is populated with the data from your URL.

You can also substitute in XmlDataDocument for XmlDocument in the example above - I prefer to use XmlDocument as it's not (yet) marked as obsolete.

I've wrapped this in a function for you:

public XmlDocument GetDataFromUrl(string url)
{
    XmlDocument urlData = new XmlDocument();
    HttpWebRequest rq = (HttpWebRequest)WebRequest.Create(url);

    rq.Timeout = 60000;

    HttpWebResponse response = rq.GetResponse() as HttpWebResponse;

    using (Stream responseStream = response.GetResponseStream())
    {
        XmlTextReader reader = new XmlTextReader(responseStream);
        urlData.Load(reader);
    }

    return urlData;

}

Simply call using:

XmlDocument document = GetDataFromUrl("http://www.globalgear.com.au/productfeed.xml");

Problem

I am doing task of loading the live xml file (from live url) to XmlDataDocument, but every time I am getting error: The operation has timed out The code is as follows, The url containing the xml feeds , I want to load it into xmlDoc. ``` XmlDataDocument xmlDoc = new XmlDataDocument(); xmlDoc.Load("http://www.globalgear.com.au/productfeed.xml"); ``` Please suggest any solution.

Original source

Related problems