我有一个从 HTTP 请求解析 XML 响应的例程,我使用 XmlDocument.LoadXml 来帮助完成这项工作。我指望此方法在错误的 XML 上抛出异常并在成功时返回加载的 XmlDocument 对象。
我没想到的是它会在加载文档时挂起几分钟。当我在测试环境中运行此代码时,它会在 100% 的时间内挂起几分钟。在我看来像是 .NET 中的一些错误...
Dim tstring As String = ""
tstring &= "" & vbCrLf
tstring &= "" & vbCrLf
tstring &= "<!DOCTYPE html PUBLIC ""-//W3C//DTD XHTML 1.0 Transitional//EN"" ""http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"">" & vbCrLf
tstring &= "" & vbCrLf
tstring &= "<html> xmlns=""http://www.w3.org/1999/xhtml"" >" & vbCrLf
tstring &= "<head><title>" & vbCrLf
tstring &= " Error" & vbCrLf
tstring &= "</title></head>" & vbCrLf
tstring &= "<body>" & vbCrLf
tstring &= "</body>" & vbCrLf
tstring &= "</html>" & vbCrLf
Dim MyXmlDoc As New XmlDocument
MyXmlDoc.LoadXml(tstring)
文档中可以删除以防止挂起的特定行是:
tstring &= "<!DOCTYPE html PUBLIC ""-//W3C//DTD XHTML 1.0 Transitional//EN"" ""http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"">" & vbCrLf
我是否必须在字符串中搜索“<!DOCTYPE html”并且在看到它时不调用 LoadXml()?我担心的是这个方法中还有什么其他陷阱在等着我?
最佳答案
loadxml 调用正在解析 doctype 以进行验证,因此它必须获取该 url - 在这种情况下速度很慢。你可以测试directly在您的浏览器中。
另一个问题provides a workaround - 引用:
in .NET 4.0 XmlTextReader has a property called DtdProcessing. When set to DtdProcessing.Ignore it should disable DTD processing.
和
doc.XmlResolver = null;
.NET 3.5 应该可以。
关于.net - XmlDocument.LoadXml() 挂了几分钟,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12428478/