使用LINQ搜索多个XML标记

时间:2014-06-19 03:54:48

标签: c# xml linq

我有一个像这样的xml:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<ViewCardResponse xmlns="http://schemas.com" schemaVersion="1.0">
    <CardActivations>
        <CardNumber>074125</CardNumber>
        <EffectiveFrom>2014-01-08</EffectiveFrom>
        <Status>InActive</Status>
      </CardActivations>
      <CardActivations>
        <CardNumber>074126</CardNumber>
        <EffectiveFrom>2014-01-08</EffectiveFrom>
        <Status>InActive</Status>
      </CardActivations>
      <CardActivations>
        <CardNumber>074127</CardNumber>
        <EffectiveFrom>2014-01-08</EffectiveFrom>
        <Status>Active</Status>
      </CardActivations>
</ViewCardResponse    >

我有3个领域。我想知道Cardnumber的值,有效期和状态为“活动”的卡片激活状态。有人可以为此建议一个LINQ查询吗?

1 个答案:

答案 0 :(得分:2)

XDocument doc=XDocument.Load(path);
var activeCards=doc.Elements()
                   .Where(x=>x.Element("Status").Value=="Active")
                   .Select(x=>
                          new 
                          {
                                CardNumber=x.Element("CardNumber").Value,
                                EffectiveFrom=x.Element("EffectiveFrom").Value
                          });