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

前端 未结 1 2053
慢半拍i
慢半拍i 2021-01-12 11:52

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

相关标签:
1条回答
  • 2021-01-12 12:36

    There is nothing built-in as far as I'm aware. You can fetch the XML content yourself with an instance of WebRequest (which you can set a timeout on) and then pass the XML data directly to the XDocument.Load method.

    Technically, the most "robust" solution would be to implement XmlResolver yourself which uses a WebRequest in the GetEntity() implementation to do a timeout. Then create an XmlReader based on your XmlResolver and pass the XmlReader to XDocument.Open.

    The reason I say that would be more "robust" is that if the XML file references other entities on the web server (e.g. a DTD) then you would probably want the timeout to apply for that as well and not just the initial request.

    0 讨论(0)
提交回复
热议问题