【问题标题】:How to read all attributes conditionally?如何有条件地读取所有属性?
【发布时间】:2016-12-29 08:54:44
【问题描述】:

我想使用 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>

【问题讨论】:

    标签: linq xelement


    【解决方案1】:

    你 System.Xml.Linq XElement 类

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

    【讨论】:

    • 谢谢@Mehmet。但是,它说 - 枚举没有价值
    猜你喜欢
    • 2020-04-25
    • 2014-01-11
    • 2019-10-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-04-15
    • 2016-08-01
    相关资源
    最近更新 更多