XmlDocument.CreateDocumentType方法中的超时

Ste*_*iga 1 .net c# xml

我正在构建一个自定义XML文档.

以下代码执行时间超过30秒(参见注释)为什么?

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);
Run Code Online (Sandbox Code Playgroud)

当这花费超过30秒时,我得到以下异常:

System.Net.WebException:

"The underlying connection was closed: The connection was closed unexpectedly."
Run Code Online (Sandbox Code Playgroud)

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 
Run Code Online (Sandbox Code Playgroud)

Nic*_*rey 5

可能是因为你的解析器试图从www.w3.org获取指定的DTD.

由于交通拥挤,他们不再直接提供DTD(请参阅此链接了解相关信息).

您需要从他们的W3C网站下载DTD,在本地缓存它们并让您的解析器将指定的URL映射到本地缓存的副本.