How to detemine namespaces used by the Xml Document

I have been browsing on the web on how to extract / determine the namespaces in the xml document given that the xml documetns are coming from different clients and there’s no standard content format, it took me a lot of days browsing to find the answer but no luck, what i found was only how to add/remove. So, what i did was experimenting / exploring the XmlDocument Object on my own, and fortunately, I found it.

Here’s the sample script on vb.net

‘Declare a variables

Dim nm as XmlNamespaceManager

Dim doc as new XmlDocument()

Dim root as XmlNode

doc.load(“c:\xml\test.xml”) ‘ xml path

nm = new XmlNamespaceManager(doc.NameTable);

Here’s the tricky part, you need to extract the attributes of the root document and iterate them.

root = doc.DocumentElement  ’get the root document

for i as integer =0 to root.Attributes.count -1

if roo.Attributes(i).Prefix.tolower() = “xmlns” then   ‘check if namespace

nm.AddNamespace (roo.Attributes(i).LocalName, roo.Attributes(i).Value)

endif

next

That’s it..

, ,

  1. No comments yet.
(will not be published)
Submit Comment

*
To prove you're a person (not a spam script), type the security word shown in the picture. Click on the picture to hear an audio file of the word.
Click to hear an audio file of the anti-spam word

Subscribe to comments feed
  1. No trackbacks yet.

SetPageWidth