如何在 XDocument.Load(string uri) 上设置超时?

发布于 2024-09-04 00:06:07 字数 172 浏览 2 评论 0原文

有没有办法在 System.Linq.Xml.XDocument.Load(string uri) 上设置超时?或者我应该使用实现 C# 通用超时中描述的技术?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

倾听心声的旋律 2024-09-11 00:06:07

据我所知,没有任何内置的东西。您可以使用 WebRequest (您可以设置超时),然后将 XML 数据直接传递到 XDocument.Load 方法。

从技术上讲,最“强大”的解决方案是实现 XmlResolver 自己在 GetEntity() 实现来执行超时。然后根据您的 XmlResolver 创建一个 XmlReader 并将 XmlReader 传递给 XDocument.Open

我说这会更“健壮”的原因是,如果 XML 文件引用 Web 服务器上的其他实体(例如 DTD),那么您可能也希望应用超时,并且不仅仅是最初的要求。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文