快速检测 Xml 命名空间 [英] Detecting Xml namespace fast

查看:33
本文介绍了快速检测 Xml 命名空间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这可能是我正在尝试解决的一个非常微不足道的问题,但我确信有更好的方法来解决.所以请放轻松.

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.

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

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.

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

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.

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

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

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

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.

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

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

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

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

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

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?

推荐答案

您可以使用 XmlReader 使用拉"方法来读取 XML(类似于 SAX 的推"方法,但更容易编码).重要的是,它不会等待读取整个文件才将内容返回给您.

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.

这篇关于快速检测 Xml 命名空间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆