如何解析XML元素?

时间:2014-05-15 12:30:34

标签: c# xml

我有一个包含此块的XML文件。除了速度和瓦特之外,我能够解析所有内容。

这是XML字符串:

"<Trackpoint>" + 
"<Time>2014-05-06T23:22:40.000Z</Time>" + 
"<Position>" + 
"<LatitudeDegrees>34.43368820473552</LatitudeDegrees>" + 
"<LongitudeDegrees>-84.17464644648135</LongitudeDegrees>" + 
"</Position>" + 
"<AltitudeMeters>377.79998779296875</AltitudeMeters>" + 
"<DistanceMeters>337.4100036621094</DistanceMeters>" + 
"<HeartRateBpm>" + 
"<Value>152</Value>" + 
"</HeartRateBpm>" + 
"<Cadence>107</Cadence>" + 
"<Extensions>" + 
"<TPX xmlns=\"http://www.garmin.com/xmlschemas/ActivityExtension/v2\">" + 
"<Speed>9.878000259399414</Speed>" + 
"<Watts>238</Watts>" +** 
"</TPX>" + 
"</Extensions>" + 
"</Trackpoint>" + 

这是我用来解析它的代码:

select new TrackPointDTO {
                        Timex = trackPointElement.Element (ns1 + "Time") != null ? Convert.ToString ((string)trackPointElement.Element (ns1 + "Time").Value) : "",
                        AltitudeMeters = (decimal)(trackPointElement.Element (ns1 + "AltitudeMeters") != null ? Convert.ToDouble ((string)trackPointElement.Element (ns1 + "AltitudeMeters").Value) : 0.0),
                        DistanceMeters = (decimal)(trackPointElement.Element (ns1 + "DistanceMeters") != null ? Convert.ToDouble ((string)trackPointElement.Element (ns1 + "DistanceMeters").Value) : 0.0),
                        HeartRateBpm = trackPointElement.Element (ns1 + "HeartRateBpm") != null ? Convert.ToInt16 ((string)trackPointElement.Element (ns1 + "HeartRateBpm").Value) : 0,
                        Cadence = trackPointElement.Element (ns1 + "Cadence") != null ? Convert.ToInt16 ((string)trackPointElement.Element (ns1 + "Cadence").Value) : 0,

                                    SpeedPower = ((from speedPowerElement in trackPointElement.Descendants (ns1 + "Extensions").Descendants(ns1 + "TPX")
                                        select new SpeedPower {
                                            Speed = (double)Convert.ToDouble ((string)speedPowerElement.Element(ns1 + "Speed").Value),
                                            Power = (double)Convert.ToDouble ((string)speedPowerElement.Element (ns1 + "Watts").Value)
                                        })
                                        .ToList ()),


                        SensorState = trackPointElement.Element (ns1 + "SensorState") != null ? trackPointElement.Element (ns1 + "SensorState").Value : "",
                        Positionx = ((from positionElement in trackPointElement.Descendants (ns1 + "Position")
                            select new Position {
                            LatitudeDegrees = (double)Convert.ToDouble ((string)positionElement.Element (ns1 + "LatitudeDegrees").Value),
                            LongitudeDegrees = (double)Convert.ToDouble ((string)positionElement.Element (ns1 + "LongitudeDegrees").Value)
                        })
                            .ToList ())

我似乎无法获得速度和力量...任何建议?

2 个答案:

答案 0 :(得分:1)

TPX元素(及其兄弟)位于不同的命名空间中。因此,您需要初始化用于这些元素的其他XNamespace变量,而不是&#34; ns1&#34;。

顺便说一下,使用XElement上的显式强制转换操作可以缩短这些行,并且使用&#34; null合并运算符&#34;:

// This:
((string)trackPointElement.Element (ns1 + "Time").Value) : "",
(decimal)(trackPointElement.Element (ns1 + "AltitudeMeters") != null ? Convert.ToDouble ((string)trackPointElement.Element (ns1 + "AltitudeMeters").Value) : 0.0),

// is equivalent to:
(string)trackPointElement.Element (ns1 + "Time") ?? ""
(decimal?)trackPointElement.Element (ns1 + "AltitudeMeters") ?? 0.0,

答案 1 :(得分:0)

您需要使用XNamespace.Get methodTPX节点使用命名空间。可能的解决方案可能如下所示:

var doc = XDocument.Parse(xml);
// create namespace of TPX
var ns = XNamespace.Get(@"http://www.garmin.com/xmlschemas/ActivityExtension/v2");
// get TPX node using the namespace
var tpx = doc.Root.Element("Extensions").Element(ns + "TPX");
// retrieve Speed and Watts using the namespace
var speed = tpx.Element(ns + "Speed");
var watts = tpx.Element(ns + "Watts");
Console.WriteLine("{0} - {1}", speed.Value, watts.Value);

输出结果为:

9.878000259399414 - 238