的XmlDocument和缓慢的架构处理架构、缓慢、XmlDocument

2023-09-04 02:44:22 作者:心病无药医

我有我需要加载到一个XmlDocument XML模板文件。例如:

  myXMLDocument.Load(myXMLFile);
 

不过,这是非常缓慢的,因为它加载在DTD。我曾经尝试都http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd和DTD的本地副本。无论采取更多或更少的同一时间。如果我把加载的DTD通过设置解析为空(例如),我然后得到错误,例如引用了未声明的实体NBSP如果文档中包含这些了。

我需要使用一个XmlDocument,因为我需要输出的文件之前,操作DOM。我怎样才能避开这些问题呢?

解决方案

您可以避开DTD,如果你返回一个空的内存流:

 私有类DummyResolver:的XmlResolver
{
   公众覆盖System.Net.ICredentials证书
   {
    组
    {
     // 没做什么。
    }
   }

公众覆盖对象GetEntity(URI绝对URI,串角色,类型ofObjectToReturn)
   {
    返回新System.IO.MemoryStream();
   }
}
 
xml语言,dtd约束是什么,xml的属性语法,xml文档的dom树的讲解

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?

解决方案

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