Home All Groups Group Topic Archive Search About

Detect Default namespace (Unkown)

Author
2 Jul 2009 7:48 AM
AGP
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

Author
2 Jul 2009 8:29 AM
Joe Fawcett
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


Show quoteHide quote
"AGP" <sindizzy.***@softhome.net> wrote in message
news:vDZ2m.3331$bq1.302@nlpi066.nbdc.sbc.com...
> 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
>
Are all your drivers up to date? click for free checkup

Author
3 Jul 2009 3:33 PM
AGP
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


Show quoteHide quote
"Joe Fawcett" <joefawcett@newsgroup.nospam> wrote in message
news:%23Nyts8u%23JHA.1608@TK2MSFTNGP02.phx.gbl...
> 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" <sindizzy.***@softhome.net> wrote in message
> news:vDZ2m.3331$bq1.302@nlpi066.nbdc.sbc.com...
>> 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
>>
>
>
>
Author
2 Jul 2009 10:55 AM
Martin Honnen
AGP wrote:
Show quoteHide quote
> 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.

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/

Bookmark and Share