如何有条件地读取所有属性? [英] How to read all attributes conditionally?
本文介绍了如何有条件地读取所有属性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想使用where子句读取所有可枚举的属性.
I want to read all attributes as I Enumerable using where clause.
所以我想要的是将Employee返回为List,其中List = PayList
So what I want is return Employee as List where 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>
Ashwani Vashishtha
Ashwani Vashishtha
推荐答案
var lists = from ls in XDocument.Load(@"C:\TEMP\Employee.xml").Descendants("Lists")
where ls.Attribute("List").Value == "PayList"
select ls;
var employees = from emp in lists.Descendants("Employee")
select new Employee
{
Start = emp.Attribute("Start").Value,
Length = emp.Attribute("Length").Value,
Name = emp.Attribute("Name").Value
};
这篇关于如何有条件地读取所有属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文