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

4

1 に答える 1

1

XmlNamespaceManager.AddNamespace()メソッドを使用して、プレフィックス (たとえば"x") を名前空間に関連付けます"http://www.garmin.com/xmlschemas/TrainingCenterDatabase/v2"

次に、XPath 式で、すべての要素名に"x"プレフィックスを付けます。

これを置き換えます

//Trackpoint

:

//x:Trackpoint

これを置き換えます

Time

:

x:Time

これを置き換えます

Position/LatitudeDegrees

と:

x:Position/x:LatitudeDegrees

これを置き換えます

Position/LongitudeDegrees

:

x:Position/x:LongitudeDegrees

最後に、これを置き換えます

HeartRateBpm

:

x:HeartRateBpm
于 2012-08-18T15:13:21.520 に答える