简体   繁体   中英

c# LinQ to xml more then one where

Can anyone help me with this code I don't know what is wrong here.. I want to retrieve Data from XML where Date is 2015/9/12 and 2015/9/13.

        var events = (from item in xdocument.Descendants("Event")
                      where (string)item.Element("Date") == "2015/9/12" &&
                      (string)item.Element("Date") == "2015/9/13"
                      select (string)item.Element("Date")).ToList();
        foreach (string name in events)
        {
            MessageBox.Show(name);
        }

Here is my XML file.

<?xml version="1.0" encoding="utf-8"?>
<Events>
  <Event ID="0">
    <Name>test</Name>
    <Date>2015/9/12</Date>
    <Priority>0</Priority>
    <Created_at>0000</Created_at>
  </Event>
  <Event ID="1">
    <Name>test1</Name>
    <Date>2015/9/13</Date>
    <Priority>0</Priority>
    <Created_at>0000</Created_at>
  </Event>
  <Event ID="2">
    <Name>test2</Name>
    <Date>2015/9/15</Date>
    <Priority>2</Priority>
    <Created_at>0000</Created_at>
  </Event>
</Events>

First thing you need to do is modify your xml. You have tag mismatches on your date tags. You have <Date>2015/9/12</Data> . Notice your ending tag says Data instead of Date. Then you can do the following:

var events = doc.Descendants("Event").Where(x => x.Element("Date").Value == "2015/9/12" || x.Element("Date").Value == "2015/9/13");

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