st78
st78

Reputation: 8316

How to specify timeout for XmlReader?

I am reading rss with xml reader.

And when url is bad it takes 60 seconds to fail for it. How i can specify timeout?

using (XmlReader reader = XmlReader.Create(url, settings))

Upvotes: 7

Views: 5597

Answers (4)

Eastman
Eastman

Reputation: 368

Another option is to do this

var settings = new XmlReaderSettings();
settings.XmlResolver = resolver;

// Create the reader.
XmlReader reader = XmlReader.Create("http://serverName/data/books.xml", settings);

Where the resolver instance is a custom class that changes the url fetching behavior as described in the documentation at the link below.

https://learn.microsoft.com/en-us/dotnet/api/system.xml.xmlurlresolver?view=net-6.0#extending-the-xmlurlresolver-class

Upvotes: 0

Andreas Vendel
Andreas Vendel

Reputation: 746

You can create your own WebRequest and create an XmlReader from the response stream. See the response to this question for details:

Prevent or handle time out with XmlReader.Create(uri)

Upvotes: 1

John Saunders
John Saunders

Reputation: 161783

Pass your own stream to the XmlReader.Create call. Set whatever timeout you like.

Upvotes: 0

Scott
Scott

Reputation: 13931

I don't know if it's possible to change the XmlReader timeout, but maybe you can do something different:

Use WebRequest to get the xml (this does have a Timeout property) and feed XmlReader this xml after you have received it:

WebRequest request = WebRequest.Create(url);
request.Timeout = 5000;

using (WebResponse response = request.GetResponse())
using (XmlReader reader = XmlReader.Create(response.GetResponseStream()))
{
    // Blah blah...
}

Upvotes: 17

Related Questions