Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Detect Default namespace (Unkown)

0 views
Skip to first unread message

AGP

unread,
Jul 2, 2009, 3:48:37 AM7/2/09
to
I am trying to parse out an XMl file and have it all working except there
are occasions whe the default namesapce will change and i need to account
for it. The namespace can be

xmlns="http://data.usgs.gov/mag/1.0"

or

xmlns="http://data.usgs.gov/mag/2.0"

or

xmlns="http://data.usgs.gov/dec/2.8"

These are examples and there could be many more.The data is more or less in
the same format as far as nodes and node names but how do I account for the
various namespaces. I will not know these ahead of time so was hoping there
was a way to parse these out of the header.

tia

AGP


Joe Fawcett

unread,
Jul 2, 2009, 4:29:11 AM7/2/09
to
That should be possible, depends on how you're processing the XML.
For example if you are using System.Xml.XmlDocument which has an identifier
of doc then you can read the doc.DocumentElement.NamepaceURI property,
assuming the default namespace is declared at the top level.

http://msdn.microsoft.com/en-us/library/system.xml.xmlelement.namespaceuri.aspx


--

Joe Fawcett (MVP - XML)
http://joe.fawcett.name


"AGP" <sindiz...@softhome.net> wrote in message
news:vDZ2m.3331$bq1...@nlpi066.nbdc.sbc.com...

Martin Honnen

unread,
Jul 2, 2009, 6:55:35 AM7/2/09
to

Do you use LINQ to XML to deal with the XML? If so then you can simply do
Dim doc As XDocument = XDocument.Load("file.xml")
Dim ns As XNamespace = doc.Root.Name.Namespace
to get an XNamespace object, assuming the default namespace is defined
on the root element of your XML documents.
Then you can use that XNamespace object ns when trying to select
elements e.g.
Dim query = _
From foo In doc.Descendants(ns + "foo") _
...
The nice thing is that the approach even works if the root element has
no namespace declaration so you don't have to change the code if you
need to deal with different documents where some have a namespace and
some do not have one.

--

Martin Honnen --- MVP XML
http://msmvps.com/blogs/martin_honnen/

AGP

unread,
Jul 3, 2009, 11:33:08 AM7/3/09
to
thanks. i think this will work. Incidentally if there are more than one
namespaeces how do I parse those out? I dont think Ill need them but i think
its good info to know.

AGP


Abel


"Joe Fawcett" <joefa...@newsgroup.nospam> wrote in message
news:%23Nyts8u%23JHA...@TK2MSFTNGP02.phx.gbl...

0 new messages