Luke
Luke

Reputation: 23700

Child of an xElement within LINQ Query

How do I get an child element of an xElement in the example below:

I am using LINQ to query some XML data, but I can't seem to get to the part of the XML from which I want the data.

Here's my code so far:

// Use LINQ to pipe the data in to OrderSummary objects
var linqQuery = from n in customer_orders.Element("ROOT").Descendants("TOEHEAD")
                select new OrderSummary
                {
                    order_date = n.Element("DATEONFILE_MV").ToString(),
                };

This code returns the order_date as:

<DATEONFILE_MV>
  <DATEONFILE>08/08/12</DATEONFILE>
</DATEONFILE_MV>

But I just want the date from within the DATEONFILE tags. At the moment it is an XElement and I can't find a way to just request the <dateonfile> element which is a child node of it.

I thought that just putting n.Element("DATEONFILE") would work, but sadly not, because it's a child node of <DATEONFILE_MV>

Upvotes: 0

Views: 646

Answers (1)

Amiram Korach
Amiram Korach

Reputation: 13296

var linqQuery = from n in customer_orders.Element("ROOT").Descendants("TOEHEAD")
                select new OrderSummary
                {
                    order_date = n.Descendants("DATEONFILE").First().Value,
                };

Upvotes: 1

Related Questions