如何有条件地阅读所有属性?

时间:2016-12-29 08:54:45

标签: linq xelement

我想使用where子句将所有属性读作IEnumerable。

所以我想要的是 -

将员工列为List = PayList

的列表

以下是我的XML。

<Employees Table="ListColumns" StartAt="1" Output="Return">
  <Lists List="PayList">
    <Employee Start="51" Length="11" Name="Amount"/>
    <Employee Start="62" Length="6" Name="Name"/>
    <Employee Start="68" Length="50" Name="Reason"/>
    <Employee Start="118" Length="7" Name="Action"/>
    <Employee Start="125" Length="6" Name="First"/>
    <Employee Start="131" Length="6" Name="Last"/>
    <Employee Start="137" Length="40" Name="Payee"/>
    <Employee Start="177" Length="6" Name="Banker"/>
    <Employee Start="183" Length="19" Name="DateIssued"/>
    <Employee Start="202" Length="19" Name="DateStopped"/>
  </Lists>
  <Lists List="ResponseList">
    <Employee Start="51" Length="11" Name="Amount"/>
    <Employee Start="62" Length="6" Name="Name"/>
    <Employee Start="68" Length="50" Name="Reason"/>
    <Employee Start="118" Length="7" Name="Action"/>
    <Employee Start="125" Length="6" Name="First"/>
    <Employee Start="131" Length="6" Name="Last"/>
    <Employee Start="137" Length="40" Name="Payee"/>
    <Employee Start="177" Length="6" Name="Banker"/>
    <Employee Start="183" Length="19" Name="DateIssued"/>
    <Employee Start="202" Length="19" Name="DateStopped"/>
  </Lists>
</Employees>

1 个答案:

答案 0 :(得分:0)

您是System.Xml.Linq XElement类

      XElement xml = XElement.Load("filePath");
      var payList = xml.Elements("Lists").Where(list => list.Attribute("List").Value == "PayList");