c# - XmlDocument.CreateDocumentType 方法超时

标签 c# .net 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);

当这超过 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/

相关文章:

xml - XPath:如何转换多个字符串?

c# - 绑定(bind)到属性并传递自定义 StringFormat

c# - 自定义样式的 TextBox SelectionStart 始终为 0

c# - 这是在 C# 中正确使用 GOTO 吗?

.net - 如何允许在 WinForm PropertyGrid 中编辑 “flag” 枚举?

c# - 如何使用 Win32 调用在 C# 中关闭/打开控制台?

java - 错误: No resource found that matches the given name (at 'background' with value '@drawable-mdpi/1.jpg' )

c# - 如何将复合外键映射到 FluentNHibernate 中的非主唯一键?

c# - 读取可能不存在的 Azure DocumentDB 文档

php - 如何在 XML 文件中连接相似的标签