6

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.

dash
  • 89,546
  • 4
  • 51
  • 71
chetan1539
  • 111
  • 1
  • 1
  • 7

4 Answers4

20

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");
dash
  • 89,546
  • 4
  • 51
  • 71
  • Hello,Thanks for your reply, I used same as you mention above, Still I am getting same error.I set the Timeout to 60000. – chetan1539 May 02 '12 at 11:27
  • Can you visit the URL from your browser and does it take a long time? If it does, increase the timeout. Do you know if you have a proxy? If you have a proxy, you may need to set this in code. – dash May 02 '12 at 11:30
  • yes I visit the url but it is not taking too long time to display the contents on the page. – chetan1539 May 02 '12 at 12:46
  • Same for me too. The above code works for me, which is why I suspect you might have a proxy issue or otherwise. Do you know if you are behind a proxy? Also, just in case something is cached somewhere, try the old trick of appending a querystring parameter to your URL - http://www.globalgear.com.au/productfeed.xml?download for example. – dash May 02 '12 at 12:47
  • No we r not using any proxy, The xml file is having total 1449 records. I put http://www.globalgear.com.au/productfeed.xml?download this also in code and I take your code as it is. Do I increase timeout bcoz file is having too much data ? – chetan1539 May 02 '12 at 13:16
  • It's hard for me to understand the exact issue here; I was able to retrieve your Xml file in about 26 seconds. Geographically, I'm in the UK, but if you can get a response through your browser fairly quickly, then it suggests something else is happening. You need to think about using a Http debugger - try http://stackoverflow.com/questions/1470634/get-http-requests-and-responses-made-using-httpwebrequest-httpwebresponse-to-sho for example as it will tell you what is going on in the background. – dash May 02 '12 at 13:23
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/10792/discussion-between-chetan1539-and-dash) – chetan1539 May 02 '12 at 13:25
1

To my knowledge there is no easy way to adjust the timeout with the method you are using.

The easiest change would be to use the webclient class and set the timeout property. This is described here http://w3ka.blogspot.co.uk/2009/12/how-to-fix-webclient-timeout-issue.html. Then use downloadfile on the webclient. Then load the saved file in the XMLDocument.

Mike Miller
  • 16,195
  • 1
  • 20
  • 27
1

Set a timeout for your web request:

using System;
using System.Net;
using System.Xml;

namespace Shelver
{
    class Program
    {
        static void Main(string[] args)
        {
            WebRequest requ = WebRequest.Create("http://www.globalgear.com.au/productfeed.xml");
            requ.Timeout = 10 * 60 * 1000; // 10 minutes timeout and not 100s as the default.
            var resp = requ.GetResponse();

            Console.WriteLine("Will download {0:N0}bytes", resp.ContentLength);
            var stream = resp.GetResponseStream();
            XmlDocument doc = new XmlDocument();
            doc.Load(stream);

        }
    }
}

This example will set it to 10 minutes.

Alois Kraus
  • 13,229
  • 1
  • 38
  • 64
  • Thanks for your reply, I set the same time as u set in above example still it is showing same error – chetan1539 May 02 '12 at 12:47
  • Do you get anything at all from the server? What happens if you add string first = sReader.ReadLine(); and print it out? Do you get anything when you open the file in the browser? – Alois Kraus May 02 '12 at 12:57
  • When I open this url in browser then it will not take too time to show the contents of xml file. – chetan1539 May 02 '12 at 13:07
  • Did you let the console app run? Does it print how many bytes it will download or does it fail before? – Alois Kraus May 02 '12 at 13:33
1

In addition to the previous answers, which should be the first step towards fixing this, I continued to get this exception despite having already loaded the response and closing the connections.

The solution for me: the Load() and LoadXml() methods would throw their own Timeout exception if the value provided wasn't actually XML. Checking to verify that the response content was XML worked in our case (this will require that the host you are getting your response from actually sets content types).

Building upon dash's answer:

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

    rq.Timeout = 60000;

    HttpWebResponse response = rq.GetResponse() as HttpWebResponse;

    // New check added to dash's answer.
    if (response.ContentType.Contains("text/xml")
    {
        using (Stream responseStream = response.GetResponseStream())
        {
            XmlTextReader reader = new XmlTextReader(responseStream);
            urlData.Load(reader);
        }
    }

    return urlData;

}
Jon G
  • 1,244
  • 1
  • 8
  • 5