在C#中从xml获取两个datetimepickers到listview的结果

时间:2014-01-06 13:59:13

标签: c# xml

我遇到了结果过滤问题。

我有一个xml文件:

<Person>
    <Client>
        <Name>JOHN</Name>
        <Sname>DOE</Sname>
        <Birthdate>12/23/1985</Birthdate>
        <City>Gjakove</City>
        <RegistrationDate>06/01/2014</RegistrationDate>
    </Client>
    <Client>
        <Name>Tim</Name>
        <Sname>DOE</Sname>
        <Birthdate>12/23/1995</Birthdate>
        <City>Gjakove</City>
        <RegistrationDate>01/01/2014</RegistrationDate>
    </Client>
</Person>

我想通过在两个datetimepickers中选择日期来过滤listview中的结果。 例如

  

DateTimePickerFROM:01/06/2013

     

DateTimePickerTO:10/10/2014

1 个答案:

答案 0 :(得分:0)

string xmlStr = @"<Root>
                    <Person>
                      <Name>JOHN</Name>
                      <Sname>DOE</Sname>
                      <Birthdate>12/23/1985</Birthdate>
                      <City>Gjakove</City>
                      <RegistrationDate>06/01/2014</RegistrationDate>
                    </Person>
                    <Person>
                      <Name>Tim</Name>
                      <Sname>DOE</Sname>
                      <Birthdate>12/23/1995</Birthdate>
                      <City>Gjakove</City>
                      <RegistrationDate>01/01/2014</RegistrationDate>
                    </Person>
                  </Root>";


var doc = XDocument.Parse(xmlStr);

var q = from el in doc.Root.Elements("Person")
        let date = DateTime.Parse(el.Element("RegistrationDate").Value)
        where date >= DateTimePickerFROM.Value && date <= DateTimePickerTO.Value
        select new ListViewItem ( el.Elements().Select(x => x.Value).ToArray());

//add all filtered Presons elements
listView1.Items.AddRange(q.ToArray());