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

主宰稳场 提交于 2019-12-05 00:55:09

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/

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
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!