質問

I have an XML File that has this block in it. I am able to parse everything except the speed and watts.

This is the XML string:

"<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>" + 

This is the code I am using to parse it:

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 ())

I can't seem to get at the speed and power...any suggestions?

役に立ちましたか?

解決

The TPX element (and its siblings) are in a different namespace. Therefore you need to initialize an additional XNamespace variable which you use for these elements instead of "ns1".

BTW, lines like these can be shortened using the explicit cast operations on XElement combined with the "null coalescing operator":

// 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,

他のヒント

You need to use the namespace for the TPX node using the XNamespace.Get method. A possible solution could look like this:

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);

The output is:

9.878000259399414 - 238
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top