简体   繁体   中英

LINQ to XML: reading XML in C#

I am trying to read an XML file using LINQ. I have had no problem reading and parsing simple XML files but this one has me stumped. Here is a portion of the file: The file is properly formed and valid.

<Activities>
    <Activity Sport="Other">
      <Id>2009-12-17T19:53:14Z</Id>
      <Lap StartTime="2009-12-17T19:53:14Z">
        <TotalTimeSeconds>820.5400000</TotalTimeSeconds>
        <DistanceMeters>1510.3433838</DistanceMeters>
        <MaximumSpeed>2.6089859</MaximumSpeed>
        <Calories>104</Calories>
        <AverageHeartRateBpm xsi:type="HeartRateInBeatsPerMinute_t">
          <Value>128</Value>
        </AverageHeartRateBpm>
        <MaximumHeartRateBpm xsi:type="HeartRateInBeatsPerMinute_t">
          <Value>139</Value>
        </MaximumHeartRateBpm>
        <Intensity>Active</Intensity>
        <TriggerMethod>Manual</TriggerMethod>

... and here is my code

XDocument document = XDocument.Load(myfileXml);

var query = from gtc in document.Descendants("Activities").Elements("Lap")
            select new
            {
                Id = gtc.Parent.Element("Id").Value,
                StartTime = gtc.Attribute("StartTime").Value,
                TotalSeconds = gtc.Element("TotalTimeSeconds").Value,
                DistanceMeters = gtc.Element("DistanceMeters").Value,
                MaximumSpeed = gtc.Element("MaximumSpeed").Value,
                Calories = gtc.Element("Calories").Value,
                Intensity = gtc.Element("Intensity").Value,
                TriggerMethod = gtc.Element("TriggerMethod").Value
            };

dataGridView1.DataSource = query.ToList();

When I run this, I see the Headers in the DataGridView, but no data. Can someone please tell me where I am going wrong? Also in the solution can someone tell me how to read the value for the heart rates? Thank you!

Change Activities to Activity:

from gtc in document.Descendants("Activity").Elements("Lap")

And for the heartrate, add these two lines near the end of your select:

    TriggerMethod = gtc.Element("TriggerMethod").Value,
    AverageHeartRateBpm = gtc.Element("AverageHeartRateBpm").Element("Value").Value,
    MaximumHeartRateBpm = gtc.Element("MaximumHeartRateBpm").Element("Value").Value
}

Here is my complete code. The only change I made apart from the two I mentioned was to remove the xsi:type attributes.

using System;
using System.Windows.Forms;
using System.Linq;
using System.Xml.Linq;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {

        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            string xml = @"<Activities>
    <Activity Sport=""Other"">
      <Id>2009-12-17T19:53:14Z</Id>
      <Lap StartTime=""2009-12-17T19:53:14Z"">
        <TotalTimeSeconds>820.5400000</TotalTimeSeconds>
        <DistanceMeters>1510.3433838</DistanceMeters>
        <MaximumSpeed>2.6089859</MaximumSpeed>
        <Calories>104</Calories>
        <AverageHeartRateBpm >
          <Value>128</Value>
        </AverageHeartRateBpm>
        <MaximumHeartRateBpm>
          <Value>139</Value>
        </MaximumHeartRateBpm>
        <Intensity>Active</Intensity>
        <TriggerMethod>Manual</TriggerMethod>
</Lap>
</Activity>
</Activities>
";
            XDocument document = XDocument.Parse(xml);

            var query = from gtc in document.Descendants("Activity").Elements("Lap")
                        select new
                        {
                            Id = gtc.Parent.Element("Id").Value,
                            StartTime = gtc.Attribute("StartTime").Value,
                            TotalSeconds = gtc.Element("TotalTimeSeconds").Value,
                            DistanceMeters = gtc.Element("DistanceMeters").Value,
                            MaximumSpeed = gtc.Element("MaximumSpeed").Value,
                            Calories = gtc.Element("Calories").Value,
                            Intensity = gtc.Element("Intensity").Value,
                            TriggerMethod = gtc.Element("TriggerMethod").Value,
                            AverageHeartRateBpm = gtc.Element("AverageHeartRateBpm").Element("Value").Value,
                            MaximumHeartRateBpm = gtc.Element("MaximumHeartRateBpm").Element("Value").Value
                        };

            dataGridView1.DataSource = query.ToList();

        }
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM