Timeout error loading Xml from URL

I am doing the task of loading a live XML file (from a live url) into an XmlDataDocument, but every time I get an error:

Operation completed

The code is as follows: The url containing the XML feeds I want to load into xmlDoc.

XmlDataDocument xmlDoc = new XmlDataDocument();
xmlDoc.Load("http://www.globalgear.com.au/productfeed.xml");

Please suggest any solution.

+5
source share
4 answers

Do not use the Load method of the XmlDataDocument class directly; you have no way to influence behavior when it comes to lengthy HTTP requests.

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

For instance:

    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);

, XmlDocument URL.

XmlDataDocument XmlDocument - XmlDocument, .

:

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;

}

, :

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

- -:

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);

        }
    }
}

10 .

+1

, , , , .

Solution for me: methods Load()and LoadXml()will generate their own timeout if the provided value is actually not XML. Verification to verify that in our case the XML content of the response has been processed (this will require that the host to which you receive the response actually sets the content types).

Based on the dash:

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 answer.
    if (response.ContentType.Contains("text/xml")
    {
        using (Stream responseStream = response.GetResponseStream())
        {
            XmlTextReader reader = new XmlTextReader(responseStream);
            urlData.Load(reader);
        }
    }

    return urlData;

}
+1
source

All Articles