Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# XML的Linq查询_C#_Xml_Linq - Fatal编程技术网

C# XML的Linq查询

C# XML的Linq查询,c#,xml,linq,C#,Xml,Linq,我有一个XML,如下所示 <Employees> <Employee> <ID>1</ID> <Address> <City>Bangalore</City> <CreatedOn>1-1-2012</CreatedOn> </Address> <Address

我有一个XML,如下所示

<Employees>
    <Employee>
        <ID>1</ID>
        <Address>
          <City>Bangalore</City>
          <CreatedOn>1-1-2012</CreatedOn>
        </Address>
        <Address>
          <City>Pune</City>
          <CreatedOn>1-10-2012</CreatedOn>
        </Address>
    </Employee>
    <Employee>
        <ID>2</ID>
        <Address>
          <City>Hyd</City>
          <CreatedOn>1-1-2009</CreatedOn>
        </Address>
        <Address>
          <City>Bombay</City>
          <CreatedOn>1-1-2010</CreatedOn>
        </Address>
    </Employee>
</Employees>

1.
班加罗尔
1-1-2012
浦那
1-10-2012
2.
液压
1-1-2009
孟买
1-1-2010
我想在C#中使用linq查询来检索具有最新createon日期的address节点的员工记录

你能帮我写Linq查询吗

提前感谢您的帮助

string xml=“1Bangalore1-1-2012Pune1-10-20122Hyd1-1-2009Bombay1-1-2010”;
string xml="<Employees><Employee><ID>1</ID><Address><City>Bangalore</City><CreatedOn>1-1-2012</CreatedOn></Address><Address><City>Pune</City><CreatedOn>1-10-2012</CreatedOn></Address></Employee><Employee><ID>2</ID><Address><City>Hyd</City><CreatedOn>1-1-2009</CreatedOn></Address><Address><City>Bombay</City><CreatedOn>1-1-2010</CreatedOn></Address></Employee></Employees>";

XElement xe = XElement.Parse(xml);

var query = xe
.Elements("Employee")
.Select
(
    x=> new 
    {
        Addresses = x.Elements("Address")
        .Select
        (
            z=>new 
            {
                ID = x.Element("ID").Value,
                City = z.Element("City").Value,
                CreatedOn = z.Element("CreatedOn").Value
            }
        ).ToList()
    }
)
.SelectMany (X=>X.Addresses);

var record = query.Where (q => DateTime.Parse(q.CreatedOn)==query.Max (x => DateTime.Parse(x.CreatedOn))).SingleOrDefault ();

Console.WriteLine("{0},{1},{2}",record.ID,record.City,record.CreatedOn);
XElement xe=XElement.Parse(xml); var query=xe .要素(“员工”) 选择 ( x=>新的 { 地址=x.元素(“地址”) 选择 ( z=>新的 { ID=x.Element(“ID”).值, 城市=z.元素(“城市”)值, CreatedOn=z.Element(“CreatedOn”).Value } )托利斯先生() } ) .SelectMany(X=>X.address); var record=query.Where(q=>DateTime.Parse(q.CreatedOn)==query.Max(x=>DateTime.Parse(x.CreatedOn)).SingleOrDefault(); WriteLine(“{0},{1},{2}”,record.ID,record.City,record.CreatedOn);
Cmon!试着谷歌一下:)有很多sampes.tl;dr 3rd world开发者希望我们为他们做功课,尽管开发者从来没有像现在这样容易得到这些琐碎问题的答案。它不会返回任何东西…我错过了关于where的部分…感谢您的回复,这将只返回1个地址值。但我要的是具有最新创建地址的独特员工。你能帮我把这个作为输出吗