I'm trying to do xpath queries over an xhtml document. Using .NET 3.5.
The document looks like this:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html lang="en" xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head>
....
</head>
<body>
...
</body>
</html>
Because the document includes various char entities (
and so on), I need to use the DTD, in order to load it with an XmlReader. So my code looks like this:
var s = File.OpenRead(fileToRead)
var reader = XmlReader.Create(s, new XmlReaderSettings{ ProhibitDtd=false });
But when I run this, it returns
An error has occurred while opening external DTD 'http://www.w3.org/TR/xhtml1-transitional.dtd': The remote server returned an error: (503) Server Unavailable.
Now, I know why I am getting the 503 error. W3C explained it very clearly.
I've seen "workarounds" where people just disable the DTD. This is what ProhibitDtd=true
can do, and it eliminates the 503 error.
But in my case that leads to other problems - the app doesn't get the entity defintions and so isn't well-formed XML. How can I validate with the DTD, and get the entity definitions, without hitting the w3.org website?
I think .NET 4.0 has a nifty built-in capability to handle this situation: the XmlPreloadedResolver. But I need a solution for .NET 3.5.
related:
- java.io.IOException: Server returned HTTP response code: 503
The answer is, I have to provide my own XmlResolver. I don't think this is built-in to .NET 3.5. That's baffling. It's also baffling that it has taken me this long to stumble onto this problem. It's also baffling that I couldn't find someone else who solved this problem already?
Ok, so.. the XmlResolver. I created a new class, derived from XmlResolver and over-rode three key things: Credentials (set), ResolveUri and GetEntity.
The documentation on this stuff is pretty skimpy, so I'll tell you what I learned. The operation of this class is like so: the XmlReader will call ResolveUri first, then, given a resolved Uri, will then call GetEntity. That method is expected to return an object of Type t (passed as a param). I have only seen it request a System.IO.Stream.
My idea is to embed local copies of the DTD and its dependencies for XHTML1.0 into the assembly, using the csc.exe
/resource
option, and then retrieve the stream for that resouce.Pretty simple. This gets called from GetEntity().
But I can improve on that. Instead of embedding the DTDs in plaintext, I gzipped them first. Then modify the above method like so:
That code opens the stream for an embedded resource, and returns a GZipStream configured for decompression. The reader gets the plaintext DTD.
What I wanted to do is resolve only URIs for DTDs from Xhtml 1.0. So I wrote the ResolveUri and GetEntity to look for those specific DTDs, and respond affirmatively only for them.
For an XHTML document with the DTD statement, the flow is like this;
XmlReader calls ResolveUri with the public URI for the XHTML DTD, which is
"-//W3C//DTD XHTML 1.0 Transitional//EN"
. If the XmlResolver can resolve, it should return... a valid URI. If it cannot resolve, it should throw. My implementation just throws for the public URI.XmlReader then calls ResolveUri with the System Identifier for the DTD, which in this case is
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"
. In this case, the XhtmlResolver returns a valid Uri.XmlReader then calls GetEntity with that URI. XhtmlResolver grabs the embedded resource stream and returns it.
The same thing happens for the dependencies - xhtml_lat1.ent, and so on. In order for the resolver to work, all those things need to be embedded.
And yes, if the Resolver cannot resolve a URI, it is expected to throw an Exception. This isn't officially documented as far as I could see. It seems a bit surprising. (An egregious violation of the principle of least astonishment). If instead, ResolveUri returns null, the XmlReader will call GetEntity on the null URI, which .... ah, is hopeless.
This works for me. It should work for anyone who does XML processing on XHTML from .NET. If you want to use this in your own applications, grab the DLL. The zip includes full source code. Licensed under the MS Public License.
You can plug it into your XML apps that fiddle with XHTML. Use it like this:
When your
ResolveUri
method gets a request for a "public" form of the URI like-//W3C//ELEMENTS XHTML Images 1.0//EN
then does your method throw and wait for the subsequent web-like URI which begins withhttp://
?Instead of throwing, I resolve the public URI to the corresponding
http://
URI (and then in myGetEntity
method I intercept requests to thehttp://
URIs).I therefore never have to throw, which I think is the right solution.
I'm using XHTML 1.1 which is 'modular' so I have to map about 40 files.
Beware that the Framework's behaviour may have changed! I have a library (including my XhtmlUrlResolver class) which is built with the .NET Framework 2, but it's invoked differently depending on whether the application (which uses the library) is build for .NET 2 or .NET 4.
With .NET 2, when my ResolveUri method always only delegated transparently to a XmlUrlResolver, then it would:
With .NET 4 there was an extra call for every resource:
*.mod
file), which my implementation just delegated to XmlUrlResolverThrowing all those WebExceptions slowed down processing a lot, which is why I revisited this to look for a fix.
Your suggestion, that I throw from ResolveUri, solved that problem, for which I thank you; but instead of throwing, returning something from ResolveUri is more elegant (and a bit faster: 40 fewer exceptions).
Here's my current source code.
You can disallow an XmlReader to open any external resources by setting the XmlReaderSettings.XmlResolver property to null.