我正在构建自定义 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);
当这超过 30 秒时,我得到以下异常:
System.Net.WebException:
"The underlying connection was closed: The connection was closed unexpectedly."
异常堆栈跟踪:
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
最佳答案
可能是因为您的解析器正在尝试从 www.w3.org 获取指定的 DTD。
由于交通繁忙,他们不再直接提供 DTD(有关信息,请参阅 this link)。
您需要从他们的 W3C 网站下载 DTD,将它们缓存在本地,并让您的解析器将指定的 URL 映射到您本地缓存的副本。
关于c# - XmlDocument.CreateDocumentType 方法超时,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4820405/