我对一个问题感到震惊。请帮帮我。
我有一个xml
<Set type="Main">
<FirstUnit xmlns:i="http://www.w3.org/2001/XMLSchema-instance" z:Id="i1" xmlns:z="http://schemas.microsoft.com/2003/10/Serialization/">
<CreateDate>2013-06-06T13:19:17.457</CreateDate>
<PrimaryKey>1</PrimaryKey>
</FirstUnit>
<Secondunit xmlns:i="http://www.w3.org/2001/XMLSchema-instance" z:Id="i1" xmlns:z="http://schemas.microsoft.com/2003/10/Serialization/">
<CreateDate>2013-06-06T13:19:17.457</CreateDate>
<PrimaryKey>1</PrimaryKey>
<Exercise>Test</Exercise>
</SecondUnit>
<FirstUnit xmlns:i="http://www.w3.org/2001/XMLSchema-instance" z:Id="i1" xmlns:z="http://schemas.microsoft.com/2003/10/Serialization/">
<CreateDate>2013-06-06T13:19:17.457</CreateDate>
<PrimaryKey>2</PrimaryKey>
</FirstUnit>
<Secondunit xmlns:i="http://www.w3.org/2001/XMLSchema-instance" z:Id="i1" xmlns:z="http://schemas.microsoft.com/2003/10/Serialization/">
<CreateDate>2013-06-06T13:19:17.457</CreateDate>
<PrimaryKey>2</PrimaryKey>
<Exercise>Test</Exercise>
</SecondUnit>
</Set>
现在我想要的只是根据主键对单元进行分组。即 FirstUnit 和 SecondUnit 应该在一个组中具有<Primarykey>
节点值“1”,在另一组中具有PrimaryKey
节点值“2”。
我已经用下面的查询试过了,还需要做更多的改进,
var elements = xDocument.GroupBy(a => a.Elements().Descendants().Where(x => x.Name.LocalName == "PrimaryKey" ).ToList());
提前致谢。