views:

71

answers:

1

Hello there,

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?

+2  A: 

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.

Dean Harding
aha! that sounds interesting, how would you detect a namespace using XmlTextReader and would it care about multiple namespaces?
Anna Tjsoken
You should be able to accomplish this with the GetNamespacesInScope method.
Garett