How to extract all namespaces information from XML

M

Martin

Hi,

I have a huge xml document to be parse to get all the namespaces
associated with each tag and to fill in XmlNamespaceManager object.
how can i do it in a most efficient way?

i am using .net and C# to code.
 
N

Nicholas Paldino [.NET/C# MVP]

Martin,

Unfortunately, because of the way that namespaces can be declared and
assigned to elements (at any point in the document, really), you have to
cycle through every node in the document and access the NamespaceURI
property. I would store this in a Dictionary so that you get unique values
(you check for existence before adding) or if you are using .NET 3.5, the
new HashSet class.
 
M

Martin Honnen

Martin said:
I have a huge xml document to be parse to get all the namespaces
associated with each tag and to fill in XmlNamespaceManager object.
how can i do it in a most efficient way?

With huge XML documents the most efficient way is always XmlReader. On
the other hand if you want to use an XmlNamespaceManager that suggests
that you want to build an XPathDocument or even XmlDocument anyway, in
that case you can do that and then use XPath to access all namespace
nodes e.g.
//namespace::*
or distinct namespace nodes e.g.
//namespace::*[not(. = ../../namespace::*)]
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top