views:

739

answers:

4

I have an xml template document that I need to load into an XmlDocument. eg

myXMLDocument.Load(myXMLFile);

However this is very slow as it loads in the dtd. I have tried both "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd" and a local copy of the dtd. Both take more or less the same time. If I turn of loading the dtd by setting the resolver to null (for example), I then get errors such as "Reference to undeclared entity 'nbsp'" if the document contains these.

I need to use an XmlDocument as I need to manipulate the DOM before outputting the document. How can I get round these problems?

A: 

Look at the DTD file, there are some more online references to .mod files, perhaps these slow the process down. You can also try to comment some of them out, some of them but not all are marked as "required" in the comments.

schnaader
I removed all but the required ones, however it didn't significantly speed it up.. :(
Sprintstar
A: 

Have you tried creating a dummy resolver which returns null for any dtd path and passing that into the load command? Something like:

class DummyResolver : XmlUrlResolver 
{
    public override Uri ResolveUri (Uri baseUri, String relativeUri) 
    {
       return null;
    }
}

XmlDocument xmlDocument = new XmlDocument();
xmlDocument.XmlResolver = new DummyResolver();

xmlDocument.Load(@"whatever.xml");
Wolfwyrd
I tried just this, but it caused xmlDocument.Load to throw an 'Object reference not set to an instance of an object' exception.. :(
Sprintstar
A: 

It's slow because it's being downloaded from the network. To fix that, do the following:

  • Download the *.mod and *.ent files referenced by the DTD (your XmlResolver instance will tell you the names of the URIs which are being looked for)
  • Add these files to your project as resource files
  • Define a subclass of XmlResolver, whose GetEntity() method returns a stream created from the local resource file
ChrisW
+2  A: 

ChrisW's answer sounds interesting, however I implemented a caching resolver from this link: http://msdn.microsoft.com/en-us/library/bb669135.aspx

That increased the speed from around 11.5s to 160ms, which is probably good enough for now. If its still not quick enough I will impliment ChrisW's solution. :)

Sprintstar
Thanks for posting that.
ChrisW