12

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 ?

abatishchev
  • 98,240
  • 88
  • 296
  • 433
Pacerier
  • 86,231
  • 106
  • 366
  • 634
  • 4
    **[TL;DR](http://en.wikipedia.org/wiki/Wikipedia:Too_long;_didn%27t_read)**... Try to find the source of this problem yourself by gradually reducing the XML until the problem disappears. After that update your question with a concise example. – Daniel Hilgarth Sep 12 '11 at 13:53
  • I am assuming that the DOCTYPE declaration is being resolved, as it will work without error if the DOCTYPE is removed. – Paul Sep 12 '11 at 14:51

2 Answers2

10

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
Edwin de Koning
  • 14,209
  • 7
  • 56
  • 74
  • 3
    This can also be done by setting the XmlResolver property of the XmlDocument to Nothing, before calling Load() or LoadXml(). – Paul Sep 12 '11 at 18:49
5

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/

Lucero
  • 59,176
  • 9
  • 122
  • 152