Nick
Nick

Reputation: 5872

Reading root element from XML using C# and LINQ

I'm reading an XML feed with the following format:

<run workoutType="standard" id="1">
    <startTime>2012-01-30T18:24:56+00:00</startTime>
</run>
<run workoutType="standard" id="2">
    <startTime>2012-01-30T18:24:56+00:00</startTime>
</run>

The following code snippet allows me to return all "runs" in the list and successfully maps the startTime field to my object.

using (XmlReader xtr = XmlTextReader.Create(string.Format("{0}?userID={1}", url, id), xrs))
{
    XDocument xd = XDocument.Load(xtr);
    return (from entry in xd.Descendants().Where(x => x.Name == "run")
            select new Run
            {
                startTime = (DateTime)entry.Element("startTime")
            }).OrderByDescending(x => x.startTime).ToList();
    xtr.Close();
}

I also need to map the run id field to my object.

How do I access this property's value?

Upvotes: 0

Views: 593

Answers (1)

Daniel Hilgarth
Daniel Hilgarth

Reputation: 174289

Add id = (int)entry.Attribute("id") after startTime = ....

Upvotes: 2

Related Questions