为什么 XmlDocument.LoadXml 抛出 System.Net.WebException?
为什么 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
XmlDocument 的内部 XmlReader 使用 XmlResolver 加载外部资源。您应该通过将
XmlResolver
设置为 null 并将DtdProcessing
设置为忽略来防止打开 DTD。这可以通过将XmlReaderSettings
对象应用到新的XmlReader
来完成。然后可以使用该读取器将 XML 加载到 XmlDocument 中。那应该可以解决你的问题。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 settingDtdProcessing
to ignore. This can be done by applying aXmlReaderSettings
object to a newXmlReader
. This reader can then be used to load the XML into the XmlDocument. That should solve your issue.埃德温给了你解决方案,我给你连接断开的原因:
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/