1

I am trying to parse an TCX document similiar to the one used in this post: Import TCX into R using XML package

Only I'm trying to use XmlDocument.SelectNodes and SelectSingleNode instead of getNodeSet. The line with xmlns looks like this:

<TrainingCenterDatabase xmlns="http://www.garmin.com/xmlschemas/TrainingCenterDatabase/v2" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.garmin.com/xmlschemas/TrainingCenterDatabase/v2 http://www.garmin.com/xmlschemas/TrainingCenterDatabasev2.xsd">

If I remove the xmlns and just have , I can parse it without any problems.

My (vb.net) code:

    Dim tcxXmlDocument As New System.Xml.XmlDocument()
    tcxXmlDocument.Load(tcxFile)
    Dim xmlnsManager = New System.Xml.XmlNamespaceManager(tcxXmlDocument.NameTable)

    Dim trackpoints As New List(Of Trackpoint)

    For Each tpXml As System.Xml.XmlNode In tcxXmlDocument.SelectNodes("//Trackpoint", xmlnsManager)
        Dim newTrackpoint As New Trackpoint

        With newTrackpoint
            .Time = tpXml.SelectSingleNode("Time").InnerText
            .LatitudeDegrees = tpXml.SelectSingleNode("Position/LatitudeDegrees").InnerText
            .LongitudeDegrees = tpXml.SelectSingleNode("Position/LongitudeDegrees").InnerText
            .HeartRateBpm = tpXml.SelectSingleNode("HeartRateBpm").InnerText
        End With

        trackpoints.Add(newTrackpoint)
    Next

How can I configure the XmlNamespaceManager so that I can access the nodes in such a tcx document?

Thanks,

Jason

Community
  • 1
  • 1
Jason
  • 174
  • 3
  • 18

1 Answers1

1

Use the XmlNamespaceManager.AddNamespace() method to associate a preffix (say "x") with the namespace "http://www.garmin.com/xmlschemas/TrainingCenterDatabase/v2".

Then in your XPath expression, pefix every element name with the "x" prefix.

Replace this:

//Trackpoint

with:

//x:Trackpoint

Replace this:

Time

with:

x:Time

Replace this:

Position/LatitudeDegrees

with:

x:Position/x:LatitudeDegrees

Replace this:

Position/LongitudeDegrees

with:

x:Position/x:LongitudeDegrees

Finally, replace this:

HeartRateBpm

with:

x:HeartRateBpm
Dimitre Novatchev
  • 240,661
  • 26
  • 293
  • 431
  • That's actually what I was looking for. I was under the false impression that the name that you give the namespace (x in your example) had to match the name as it exists in the xml file (blank in this case). Now I know better. – Jason Aug 18 '12 at 18:18