我有一个XML模板文档,需要将其加载到XmlDocument中。例如

myXMLDocument.Load(myXMLFile);

但是,这非常缓慢,因为它会加载到dtd中。我已经尝试了"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd"和dtd的本地副本。两者花费的时间大致相同。如果我通过将解析器设置为null来加载dtd(例如),那么如果文档中包含这些错误,则会收到诸如"Reference to undeclared entity 'nbsp'"之类的错误。

我需要使用XmlDocument,因为我需要在输出文档之前操纵DOM。我该如何解决这些问题?

最佳答案

如果返回空的内存流,则可以避免DTD:

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

10-08 10:57
查看更多