如何在XML中搜索嵌套元素值

时间:2015-12-07 09:40:51

标签: c# xml linq-to-xml xelement

我浏览了一个xml文件。这是它的一个示例。

<Cproducts>
   <ID>id_one</ID>
   <Name>name_one</Name>
   <Availability>
        <Departure>
          <Date>2015-12-03T00:00:00.0000000+00:00</Date>
        </Departure>
        <Departure>
          <Date>2015-12-06T00:00:00.0000000+00:00</Date>
        </Departure>
    <Availability>
    <ID>id_two</ID>
   <Name>name_two</Name>
   <Availability>
        <Departure>
          <Date>2015-12-03T00:00:00.0000000+00:00</Date>
        </Departure>
        <Departure>
          <Date>2015-12-03T00:00:00.0000000+00:00</Date>
        </Departure>
        <Departure>
          <Date>2015-12-07T00:00:00.0000000+00:00</Date>
        </Departure>
        <Departure>
          <Date>2015-12-08T00:00:00.0000000+00:00</Date>
        </Departure>
    <Availability>
 </Cproducts>   

我直接搜索名字。

 var getneededData = rootele.Elements("CruiseProduct")
                                .Where(l => l.Element("Name").Value == "some value");

然后getneededData仅提供Name == "some value"的数据。我想查看Date ="some date"所在的日期。我怎样才能像Name一样直接这样做?

3 个答案:

答案 0 :(得分:0)

只需添加包含date元素

的条件或条件
var anotherDate = DateTime.Now ; 
var getneededData = rootele.Elements("CruiseProduct")
           .Where(l => l.Element("Name").Value == "some value" || DateTime.ParseExact(l.Descendants("Date").First().Value, "o") == anotherDate);

答案 1 :(得分:0)

您的XML与您的代码不符。我假设您的XML看起来像这样

<?xml version="1.0" encoding="UTF-8" ?>
<Cproducts>
    <CruiseProduct>
        <ID>id_one</ID>
        <Name>name_one</Name>
        <Availability>
            <Departure>
                <Date>2015-12-03T00:00:00.0000000+00:00</Date>
            </Departure>
            <Departure>
                <Date>2015-12-06T00:00:00.0000000+00:00</Date>
            </Departure>
        </Availability>
    </CruiseProduct>
    <CruiseProduct>
        <ID>id_two</ID>
        <Name>name_two</Name>
        <Availability>
            <Departure>
                <Date>2015-12-03T00:00:00.0000000+00:00</Date>
            </Departure>
            <Departure>
                <Date>2015-12-03T00:00:00.0000000+00:00</Date>
            </Departure>
            <Departure>
                <Date>2015-12-07T00:00:00.0000000+00:00</Date>
            </Departure>
            <Departure>
                <Date>2015-12-08T00:00:00.0000000+00:00</Date>
            </Departure>
        </Availability>
    </CruiseProduct>
</Cproducts>   

假设您可以找到“名称”为

的产品
var name_one_product = xDoc.Descendants("CruiseProduct")
                           .Where(cp => cp.Element("Name").Value == "name_one");

以“日期”为

var date = new DateTime(2015, 12, 7);
var product_2015_12_07 = xDoc.Descendants("CruiseProduct")
                             .Where(cp => cp.Descendants("Date")
                                            .Any(d => DateTime.Parse(d.Value).Date.Equals(date.Date)));

如果你想要两个条件,那么

var date = new DateTime(2015, 12, 7); //the date on which you want to filter
var product_2015_12_07 = xDoc.Descendants("CruiseProduct")
                                .Where(cp => cp.Element("Name").Value == "name_one" ||
                                             cp.Descendants("Date")
                                               .Any(d => DateTime.Parse(d.Value).Date.Equals(date.Date)));

使用||&&以您的目的为准。

答案 2 :(得分:0)

这就是我所做的,这对我有用....

 var getneededData = rootele.Elements("CruiseProduct")
                                .Where(l => l.Element("Location").Value == "some destination" && l.Element("Types").Element("Type").Value == "some type" && l.Descendants("Date").Any(d=> d.Value.Substring(0,10).Equals("some date")));