简体   繁体   English

LINQ查询中xElement的子级

[英]Child of an xElement within LINQ Query

How do I get an child element of an xElement in the example below: 在下面的示例中,如何获取xElement的子元素:

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. 我正在使用LINQ来查询一些XML数据,但是我似乎无法到达需要数据的XML部分。

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: 此代码将order_date返回为:

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

But I just want the date from within the DATEONFILE tags. 但是我只想要DATEONFILE标记中的日期。 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. 目前,它是一个XElement,我无法找到一种方法来请求它的子节点<dateonfile>元素。

I thought that just putting n.Element("DATEONFILE") would work, but sadly not, because it's a child node of <DATEONFILE_MV> 我以为只将n.Element("DATEONFILE")可以了,但是遗憾的是,因为它是<DATEONFILE_MV>的子节点

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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