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
"James R. Brown" wrote in message news:DNydnZgypflFp-HVnZ2dnUVZ_tbinZ2d@earthlink.com... > 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 > 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
"James R. Brown" wrote in message news:DNydnZgypflFp-HVnZ2dnUVZ_tbinZ2d@earthlink.com... >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 > > 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
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" wrote in message news:e8OKm6k5IHA.1176@TK2MSFTNGP02.phx.gbl... > "James R. Brown" wrote in message > news:DNydnZgypflFp-HVnZ2dnUVZ_tbinZ2d@earthlink.com... >> 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 >> > > 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 > >
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" <joefawcett@newsgroup.nospam> wrote in message news:%23CwUS%23k5IHA.1428@TK2MSFTNGP06.phx.gbl... > "James R. Brown" wrote in message > news:DNydnZgypflFp-HVnZ2dnUVZ_tbinZ2d@earthlink.com... >>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 >> >> > 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 > > >