How to extract all namespaces information from XML

  • Thread starter Thread starter Martin
  • Start date Start date
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.
 
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.
 
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::*)]
 
Back
Top