我正在尝试解析与这篇文章中使用的类似的 TCX 文档:Import TCX into R using XML package
只有我在尝试使用 XmlDocument.SelectNodes 和 SelectSingleNode 而不是 getNodeSet。带有 xmlns 的行如下所示:
<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">
如果我删除 xmlns 并且只有 ,我可以毫无问题地解析它。
我的(vb.net)代码:
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
如何配置 XmlNamespaceManager 以便可以访问此类 tcx 文档中的节点?
谢谢,
杰森