2012-03-31 45 views
1
<lap1> 
    <Track> 
    <Trackpoint> 
    <Time>2012-03-29T08:06:57Z</Time> 
    <Position> 
     <LatitudeDegrees>46.2950333</LatitudeDegrees> 
     <LongitudeDegrees>7.1046736</LongitudeDegrees> 
    </Position> 
    <AltitudeMeters>1782.4216309</AltitudeMeters> 
<DistanceMeters>0.0000000</DistanceMeters> 
<HeartRateBpm xsi:type="HeartRateInBeatsPerMinute_t"> 
    <Value>112</Value> 
</HeartRateBpm> 
<SensorState>Absent</SensorState> 
    </Trackpoint> 
    <Trackpoint> 
     <Time>2012-03-29T08:06:58Z</Time> 
    <Position> 
     <LatitudeDegrees>46.2950271</LatitudeDegrees> 
     <LongitudeDegrees>7.1046776</LongitudeDegrees> 
    </Position> 
    <AltitudeMeters>1782.4216309</AltitudeMeters> 
    <DistanceMeters>0.1714377</DistanceMeters> 
<HeartRateBpm xsi:type="HeartRateInBeatsPerMinute_t"> 
    <Value>112</Value> 
</HeartRateBpm> 
<SensorState>Absent</SensorState> 
    </Trackpoint> 
    </Track> 
</lap1> 

我有這樣的一段XML從我的GPS設備和IM嘗試將其解析到一個列表中的軌道解析XML和GPS數據,列出

public class track 
{ 
    public float positionx; 
    public float positiony; 
    public float altitude; 
    public DateTime Time; 
} 

已經沒有做太多的XML的工作,我一直在閱讀各種XML的方法,包括XPath和LinqToXML,但即時通訊掙扎解析XML,特別是「位置和其子值

我將是在這一個

回答

3

這個S的任何幫助非常感謝應該訣竅:

XDocument doc = XDocument.Load("file.xml"); 

var tracks = (from elem in doc.Root.Descendants("Trackpoint") 
       select new track() 
       { 
        Time = DateTime.Parse(elem.Element("Time").Value, CultureInfo.InvariantCulture), 
        positionx = float.Parse(elem.Element("Positition").Element("LatitudeDegrees").Value, CultureInfo.InvariantCulture), 
        positiony = float.Parse(elem.Element("Positition").Element("LongitudeDegrees").Value, CultureInfo.InvariantCulture), 
        altitude = float.Parse(elem.Element("AltitudeMeters").Value, CultureInfo.InvariantCulture), 
       }); 

告訴我,如果它的工作!

+0

應該是「Trackpoint」導致後代擴展方法區分大小寫。 – MarcinJuraszek 2012-03-31 23:39:48