为什么 XmlDocument.LoadXml 抛出 System.Net.WebException?

发布于 2024-12-04 07:13:00 字数 662 浏览 1 评论 0原文

为什么 System.Xml.XmlDocument.LoadXml 方法会抛出 System.Net.WebException

这真是令人难以置信的疯狂,如果 MSDN是的,LoadXml 最多应该给我一个 System.Xml.XmlException

但我有一些奇怪的例外,例如:

底层连接已关闭:连接意外关闭。

Dim document As New XmlDocument
document.LoadXml("<!DOCTYPE html PUBLIC ""-//W3C//DTD XHTML 1.0 Transitional//EN"" ""http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd""><x></x>")
MsgBox(document.LastChild.Name)

到底是什么导致了异常?

Why does System.Xml.XmlDocument.LoadXml method throw System.Net.WebException ?

This is really mind boggling crazy, if MSDN was right, LoadXml should at most give me a System.Xml.XmlException.

Yet I have weird exceptions like:

The underlying connection was closed: The connection was closed unexpectedly.

Dim document As New XmlDocument
document.LoadXml("<!DOCTYPE html PUBLIC ""-//W3C//DTD XHTML 1.0 Transitional//EN"" ""http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd""><x></x>")
MsgBox(document.LastChild.Name)

What on earth is causing the exception ?

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

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

发布评论

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

评论(2

尘曦 2024-12-11 07:13:00

XmlDocument 的内部 XmlReader 使用 XmlResolver 加载外部资源。您应该通过将 XmlResolver 设置为 null 并将 DtdProcessing 设置为忽略来防止打开 DTD。这可以通过将 XmlReaderSettings 对象应用到新的 XmlReader 来完成。然后可以使用该读取器将 XML 加载到 XmlDocument 中。那应该可以解决你的问题。

    Dim doc As New XmlDocument()
    Dim settings As New XmlReaderSettings()
    settings.XmlResolver = Nothing
    settings.DtdProcessing = DtdProcessing.Ignore

    Using sr As New StringReader("<!DOCTYPE html PUBLIC ""-//W3C//DTD XHTML 1.0 Transitional//EN"" ""http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd""><x></x>")
        Using reader As XmlReader = XmlReader.Create(sr, settings)
            doc.Load(reader)
        End Using
    End Using

The internal XmlReader of a XmlDocument uses a XmlResolver to load external resources. You should prevent the opening of the DTD by setting the XmlResolver to null and setting DtdProcessing to ignore. This can be done by applying a XmlReaderSettings object to a new XmlReader. This reader can then be used to load the XML into the XmlDocument. That should solve your issue.

    Dim doc As New XmlDocument()
    Dim settings As New XmlReaderSettings()
    settings.XmlResolver = Nothing
    settings.DtdProcessing = DtdProcessing.Ignore

    Using sr As New StringReader("<!DOCTYPE html PUBLIC ""-//W3C//DTD XHTML 1.0 Transitional//EN"" ""http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd""><x></x>")
        Using reader As XmlReader = XmlReader.Create(sr, settings)
            doc.Load(reader)
        End Using
    End Using
风吹短裙飘 2024-12-11 07:13:00

埃德温给了你解决方案,我给你连接断开的原因:

http://www.w3.org/blog/systeam/2008/02/08/w3c_s_excessive_dtd_traffic/

Edwin gave you the solution, and I'm giving you the reason for the connection drop:

http://www.w3.org/blog/systeam/2008/02/08/w3c_s_excessive_dtd_traffic/

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