XmlDocument and slow schema processing

我与影子孤独终老i 提交于 2019-12-05 09:43:22

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();
   }
}

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

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.

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

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