XmlDocument 和缓慢的架构处理
我有一个 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
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. :)
如果返回空内存流,则可以避免 DTD:
You can avoid the DTD if you return an empty memory stream:
查看 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.
它很慢,因为它是从网络下载的。 要解决此问题,请执行以下操作:
It's slow because it's being downloaded from the network. To fix that, do the following:
您是否尝试过创建一个虚拟解析器,该解析器为任何 dtd 路径返回 null 并将其传递到加载命令中? 就像是:
Have you tried creating a dummy resolver which returns null for any dtd path and passing that into the load command? Something like: