XmlDocument 和缓慢的架构处理

发布于 2024-07-11 05:29:14 字数 387 浏览 7 评论 0原文

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

myXMLDocument.Load(myXMLFile);

但是这非常慢,因为它加载到 dtd 中。 我已经尝试了 "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd" 和 dtd 的本地副本。 两者花费的时间或多或少相同。 如果我通过将解析器设置为 null(例如)来加载 dtd,那么如果文档包含这些错误,我就会收到诸如 “引用未声明的实体 'nbsp'” 之类的错误。

我需要使用 XmlDocument,因为我需要在输出文档之前操作 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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

千纸鹤 2024-07-18 05:29:14

ChrisW 的答案听起来很有趣,但是我通过以下链接实现了缓存解析器: http:// /msdn.microsoft.com/en-us/library/bb669135.aspx

这将速度从大约 11.5 秒提高到 160 毫秒,目前可能已经足够了。 如果它仍然不够快,我会暗示 ChrisW 的解决方案。 :)

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

难忘№最初的完美 2024-07-18 05:29:14

如果返回空内存流,则可以避免 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();
   }
}

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();
   }
}
很酷又爱笑 2024-07-18 05:29:14

查看 DTD 文件,还有一些对 .mod 文件的在线引用,也许这些会减慢进程。 您也可以尝试将其中一些注释掉,其中一些但并非全部在注释中标记为“必需”。

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.

待"谢繁草 2024-07-18 05:29:14

它很慢,因为它是从网络下载的。 要解决此问题,请执行以下操作:

  • 下载 DTD 引用的 *.mod 和 *.ent 文件(您的 XmlResolver 实例将告诉您正在查找的 URI 的名称)
  • 将这些文件作为资源文件添加到您的项目中
  • 定义XmlResolver 的子类,其 GetEntity() 方法返回从本地资源文件创建的流

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
陪我终i 2024-07-18 05:29:14

您是否尝试过创建一个虚拟解析器,该解析器为任何 dtd 路径返回 null 并将其传递到加载命令中? 就像是:

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

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");
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文