XmlDocument and slow schema processing

╄→гoц情女王★ 提交于 2019-12-22 06:40:37

问题


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?


回答1:


You can avoid the DTD if you return an empty memory stream:

private class DummyResolver : XmlResolver
{
   public override System.Net.ICredentials Credentials
   {
    set
    {
     // Do nothing.
    }
   }

public override object GetEntity(Uri absoluteUri, string role, Type ofObjectToReturn)
   {
    return new System.IO.MemoryStream();
   }
}



回答2:


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. :)




回答3:


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.




回答4:


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



回答5:


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");


来源:https://stackoverflow.com/questions/423921/xmldocument-and-slow-schema-processing

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