问题描述
有没有一种方法来设置System.Linq.Xml.XDocument.Load(字符串URI)超时?或者我应该使用http://stackoverflow.com/questions/299198/implement-c-generic-timeout?
Is there a way to set a timeout on System.Linq.Xml.XDocument.Load(string uri)? Or should I use the technique described in http://stackoverflow.com/questions/299198/implement-c-generic-timeout?
推荐答案
没有什么内置据我所知。您可以获取XML内容自己与的WebRequest 的一个实例(您可以的设置超时),然后直接将XML数据的 XDocument.Load
方法。
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.
从技术上讲,最稳健的解决办法是实行的XmlResolver 自己它采用了的WebRequest
中的的实施做了超时。然后创建一个的XmlReader
根据你的的XmlResolver
并通过的XmlReader
到 XDocument.Open
。
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
.
我说,会更稳健的原因是,如果XML文件引用的等的Web服务器上的实体(如DTD),那么你可能会想超时办理该还有,不只是初始请求。
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.
这篇关于如何设置超时的XDocument.Load(字符串URI)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!