Detecting Xml namespace fast

霸气de小男生 提交于 2019-12-12 03:46:10

问题


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?


回答1:


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.



来源:https://stackoverflow.com/questions/2846265/detecting-xml-namespace-fast

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!