Steven Striga
Steven Striga

Reputation: 1381

Timeout in XmlDocument.CreateDocumentType method

I am building a custom XML document.

The following code takes upwards of 30 seconds to execute (see comments) Why?

var doc = new XmlDocument(); 
doc.AppendChild(doc.CreateXmlDeclaration("1.0", "utf-8", null));

// hangs on next line - doc.CreateDocumentType()
var xmlDocType = doc.CreateDocumentType(
            "svg"
            , "-//W3C//DTD SVG 20001102//EN"
            , "http://www.w3.org/TR/2000/CR-SVG-20001102/DTD/svg-20001102.dtd"
            , null);

doc.AppendChild(xmlDocType);

When this takes over 30 seconds, I get the following exception:

System.Net.WebException:

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

Exception Stack Trace:

at System.Net.HttpWebRequest.GetResponse()
at System.Xml.XmlDownloadManager.GetNonFileStream(Uri uri, ICredentials credentials, IWebProxy proxy, RequestCachePolicy cachePolicy)
at System.Xml.XmlDownloadManager.GetStream(Uri uri, ICredentials credentials, IWebProxy proxy, RequestCachePolicy cachePolicy)
at System.Xml.XmlUrlResolver.GetEntity(Uri absoluteUri, String role, Type ofObjectToReturn)
at System.Xml.XmlTextReaderImpl.OpenAndPush(Uri uri)
at System.Xml.XmlTextReaderImpl.PushExternalEntityOrSubset(String publicId, String systemId, String 

Upvotes: 1

Views: 864

Answers (1)

Nicholas Carey
Nicholas Carey

Reputation: 74287

Probably because your resolver is trying to fetch the specified DTD from www.w3.org.

They're not serving up the DTDs directly any more due to heavy traffic (see this link for info).

You need to download the DTDs from their W3C web site, cache them locally and have your resolver map the specified URL to your locally cached copy.

Upvotes: 5

Related Questions