Creating an XPathDocument with referenced DTD sometimes throws a web exception. Why?
Asked
Active
Viewed 800 times
3 Answers
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.

John Lemp
- 5,029
- 3
- 28
- 36
-
But the MSDN should clearly state this might happen. Looking at the XPathDocument you can't tell it will look for DTD on the web. Also why is the exception thrown? If DTD is not needed - works offline doesn't it? Thanks for the suggestions anyway - I'll try them next time! – Goran Oct 28 '08 at 07:22
1
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);

Aussie Ash
- 1,276
- 12
- 10
0
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!

Goran
- 6,798
- 9
- 41
- 57