Why do I get web exception when creating an XPathDocument?

坚强是说给别人听的谎言 提交于 2019-12-24 06:32:03

问题


Creating an XPathDocument with referenced DTD sometimes throws a web exception. Why?


回答1:


You can write a custom XmlUrlResolver and then ignore the remote DTD. Also, I believe you can set use XmlResolver = null on the XmlTextReader.




回答2:


for those interested I've found a workaround to disable the dtd check

XmlReaderSettings settings = new XmlReaderSettings();
 settings.XmlResolver = null;
 settings.ProhibitDtd = false;

 var xmlReader = XmlTextReader.Create(new StringReader(xmlString),settings);
 XPathDocument xpathDoc = new XPathDocument(xmlReader);



回答3:


See http://todotnet.com/archive/2006/07/27/8248.aspx

Because in the construction of XPathDocument, there's an http GET command to see if it can access the DTD. It's not doing anything with the DTD. It's for just in case. So while XPathDocument is initially set up to be a faster alternative to XmlDocument, you'll have the additional overhead of an http request that needs to be resolved. Imagine that server being on the other side of the globe!



来源:https://stackoverflow.com/questions/239788/why-do-i-get-web-exception-when-creating-an-xpathdocument

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!