views:

110

answers:

3

Why is my GetEntity function in my overloaded XmlResolver being passed the Formal Public Identifier when I load an xml file into an XmlDocument? Is this a bug or am I supposed to deal with this some how?

edit: Here's some code.

Say for example I do this:

XmlDocument myXmlDoc = new XmlDocument();
myXmlDoc.XmlResolver = new MyXmlResolver();
myXmlDoc.Load("myxmlfile.xml");

In MyXmlResolver I have the following code:

public override object GetEntity(Uri absUri, string role, Type typeToRet)
{
  if (typeToRet == null || typeToRet == typeof(Stream))
     return GetStream(absUri);
  else
     throw new XmlException("Unsupported class type: " + typeToRet);
}

I get passed "file://path/to/xmldoc/-//W3C//DTD XHTML 1.1//EN", in absUri which doesn't make any sense to me. Am I just supposed to ignore it?

A: 

Do you really need a custom XmlResolver? What are you doing in your resolver that warrants a custom implementation?

The reason you are being passed a URI like that is because you have told the XmlDocument that you will explicitly resolve all external references in the document (like DTDs). If you don't need to do that manually then you should use the existing XmlResolver type or just leave it null and let the default behavior occur.

Andrew Hare
A: 

I am using one to locally cache the dtd, mod files etc (see this question). My problem with it doing what it does, is that the FPI it passes me isn't really an external reference, and I can't distinguish it from a real external reference.

Sprintstar
What does the source XML look like? The FPI will pass you the DTD from the document to resolve.
Andrew Hare
<?xml version="1.0" encoding="utf-8" ?><!DOCTYPE htmlPUBLIC "-//W3C//DTD XHTML 1.1//EN""http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd"><html xmlns='http://www.w3.org/1999/xhtml' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' xml:lang='en' >etc...
Sprintstar
I get passed "-//W3C//DTD XHTML 1.1//EN", then I get passed "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd"..
Sprintstar
A: 

I am having the same issue. What I did was check the value passed in, and if it was a bad value, I returned it a valid uri for a dummy DTD I created. Just calling LoadXML works when the proper dtd is beside the doc. It knows how to see just the DTD and ignore the PUBLIC portion that is blowing up the GetEntity method. I want the DTD in a local common place, so that I do not need to have it living with the docs in various places. Therefore I override GetEntity, give it the common path, and then I can use one DTD for many docs that live all over the Filesystem.

Kilimanjaro