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
--
Joe Fawcett (MVP - XML)
"Anthony Jones" <A...@yadayadayada.com> wrote in message
news:e8OKm6k5...@TK2MSFTNGP02.phx.gbl...
"Joe Fawcett" <joefa...@newsgroup.nospam> wrote in message
news:%23CwUS%23k5IH...@TK2MSFTNGP06.phx.gbl...