快速检测 Xml 命名空间

发布于 2024-09-01 18:04:23 字数 727 浏览 3 评论 0原文

这可能是我试图解决的一个非常微不足道的问题,但我确信有更好的方法来做到这一点。所以请对我宽容一些。

我有一堆 XSD 文件是我们应用程序内部的,我们有大约 20-30 个 Xml 文件,它们根据这些 XSD 实现数据集。一些 Xml 文件很小 (<100Kb),其他文件约为 3-4Mb,少数超过 10Mb。

我需要找到一种方法来确定这些 Xml 文件的命名空间,以便提供(类似的)基于 XSD 的智能感知。这的实现不是问题——另一位开发人员已经为此编写了代码。

但我不确定检测命名空间的最佳(也是最快!)方法是不使用 XmlDocument(它进行完整解析)。

我使用的是 C# 3.5,文档以流的形式传输(有些是远程文件)。所有文件都是 *.xml (我可以检测它是否基于扩展名),但不幸的是 Xml 命名空间是唯一的方法。

现在我已经尝试过 XmlDocument,但我发现它效率低下且缓慢,因为较大的文档正在等待解析(甚至是 100Kb 文档)。

public string GetNamespaceForDocument(Stream document);

类似上面的东西是我的方法签名 - 重载包括“内容”的字符串。 RegEx(编译)模式会好吗?

Visual Studio 如何如此有效地管理此问题?另一所大学告诉我在 C/C++ 中找到一个快速的 Xml 解析器,解析内容并有一个存根来返回命名空间,因为它在 .NET 中速度较慢,这是一个好主意吗?

This may be a very trivial problem I'm trying to solve, but I'm sure there's a better way of doing it. So please go easy on me.

I have a bunch of XSD files that are internal to our application, we have about 20-30 Xml files that implement datasets based off those XSDs. Some Xml files are small (<100Kb), others are about 3-4Mb with a few being over 10Mb.

I need to find a way of working out what namespace these Xml files are in order to provide (something like) intellisense based off the XSD. The implementation of this is not an issue - another developer has written the code for this.

But I'm not sure the best (and fastest!) way of detecting the namespace is without the use of XmlDocument (which does a full parse).

I'm using C# 3.5 and the documents come through as a Stream (some are remote files). All the files are *.xml (I can detect if it was extension based) but unfortunately the Xml namespace is the only way.

Right now I've tried XmlDocument but I've found it to be innefficient and slow as the larger documents are awaiting to be parsed (even the 100Kb docs).

public string GetNamespaceForDocument(Stream document);

Something like the above is my method signature - overloads include string for "content". Would a RegEx (compiled) pattern be good?

How does Visual Studio manage this so efficiently? Another college has told me to find a fast Xml parser in C/C++, parse the content and have a stub that gives back the namespace as its slower in .NET, is this a good idea?

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

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

发布评论

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

评论(1

夜访吸血鬼 2024-09-08 18:04:23

您可以使用 XmlReader 它使用“拉”读取 XML 的方法(类似于 SAX 的“push”方法,但更容易编写代码)。重要的是,它不会等到读取整个文件才将内容返回给您。

You can use XmlReader which uses a "pull" method to read the XML (similar to SAX's "push" method, but a little easier to code against). The important thing is, it doesn't wait to read the whole file before returning stuff to you.

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