How to set timeout on XDocument.Load (uri string)?

Is there a way to set a timeout in System.Linq.Xml.XDocument.Load (uri string)? Or should I use the technique described in Implement C # Common Timeout ?

+6
c # timeout linq-to-xml
source share
1 answer

In my opinion, there is nothing built in. You can get the XML content yourself using a WebRequest instance (which you can set a timeout for), and then pass the XML data directly to XDocument.Load .

Technically, the most β€œreliable” solution would be to implement an XmlResolver that uses WebRequest in GetEntity () to timeout. Then create an XmlReader based on the XmlResolver and pass the XmlReader to XDocument.Open .

The reason I will say that it would be more "reliable" is because if the XML file refers to other objects on the web server (for example, DTD), you probably need a timeout for this, and not just initial request.

+7
source share

All Articles