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

Xpath and Namespaces

3 views
Skip to first unread message

James R. Brown

unread,
Jul 15, 2008, 1:51:05 AM7/15/08
to
I am trying to read a few elements from an xml file that normally resides on
some Garmin GPS Devices. I am using vb.net. I have written a rather long and
messy routine that gets the information I need using the XmlTextReader
class. However, I would like to write a much shorter version using the Dom
and .SelectSingleNode using Xpath expressions.

The xml file I am reading has defined namespaces that seem to keep xpath
from working. Example: <Device
xmlns="http://www.garmin.com/xmlschemas/GarminDevice/v2"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.garmin.com/xmlschemas/GarminDevice/v2
http://www.garmin.com/xmlschemas/GarminDevicev2.xsd">.

I have read more than once that I can get xpath to work by adding a dummy
namespace. Example: nsMgr.AddNamespace("test",
"http://tempuri.org/test.xsd"). This did not help. Obviously I am a beginner
operating outside my level of expertise. Below is some code. All I get is
"Object reference not set to an instance of an object." Can someone tell me
what I am failing to grasp?

Thanks in advance.

Imports System.Xml
Module Module1

Sub Main()
Dim GarminDevice As New XmlDocument
Dim nsMgr As XmlNamespaceManager
Dim xml_Node As XmlNode

'Temporary location for testing. This file is normally on the
device.
GarminDevice.Load("C:\Documents and Settings\James R. Brown\My
Documents\GarminDevice.xml")

nsMgr = New XmlNamespaceManager(GarminDevice.NameTable)
nsMgr.AddNamespace("test", "http://tempuri.org/test.xsd")

xml_Node = GarminDevice.SelectSingleNode("//test:Description",
nsMgr)

Try
Console.WriteLine(xml_Node.InnerText)
Catch
MsgBox(Err.Description)
Err.Clear()
End Try
Console.ReadKey()


End Sub

End Module


Anthony Jones

unread,
Jul 15, 2008, 3:44:42 AM7/15/08
to
"James R. Brown" <heavy...@earthlink.net> wrote in message
news:DNydnZgypflFp-HV...@earthlink.com...

Its not a case of adding a 'Dummy' namespace, but you need specify an alias
fora namespace the you wish to query in XPath.

nsMgr.AddNamespace("t", "http://www.garmin.com/xmlschemas/GarminDevice/v2")

xml_Node = GarminDevice.SelectSingleNode("//t:Description", nsMgr)


--
Anthony Jones - MVP ASP/ASP.NET


Joe Fawcett

unread,
Jul 15, 2008, 3:51:19 AM7/15/08
to
"James R. Brown" <heavy...@earthlink.net> wrote in message
news:DNydnZgypflFp-HV...@earthlink.com...
The advice to add a dummy namespace/prefix mapping should add that the
namepace needs to be the same as in the document. So for the example
document you need to add:
nsMgr.AddNamespace("default",
"http://www.garmin.com/xmlschemas/GarminDevice/v2");
The choice of "default" is entirely arbitrary.
Then all unprefixed elements that need to be selected use the "default"
prefix.

--

Joe Fawcett (MVP - XML)

http://joe.fawcett.name

James R. Brown

unread,
Jul 15, 2008, 10:29:09 AM7/15/08
to
You are absolutely correct! Thank you so much! It works perfect now and I
have learned something useful as well. You have my undying gratitude!

"Anthony Jones" <A...@yadayadayada.com> wrote in message
news:e8OKm6k5...@TK2MSFTNGP02.phx.gbl...

James R. Brown

unread,
Jul 15, 2008, 10:35:53 AM7/15/08
to
Thank you all for your help. Both you and Anthony Jones were absolutely
right. I wish I had checked with you guys earlier.

"Joe Fawcett" <joefa...@newsgroup.nospam> wrote in message
news:%23CwUS%23k5IH...@TK2MSFTNGP06.phx.gbl...

0 new messages