<Employee>
<Data>
<Details type = "Personal">
<Detail Name ="John" Associate Job="Job">
<Department Name="Law" >
<Branch>New York</Branch>
<Branch>Florida</Branch>
</Department>
<Department Name="Lecture" >
<Branch>London</Branch>
<Branch>Brit</Branch>
</Department>
</Detail>
</Details>
</Data>
</Employee>
输出
Law -- New York, Florida Lecture -- London, Brit
上述XML格式的Lambda表达式:---
var employee =(从document.Descendants(“ Detail”)中的r开始。其中(r =>(string)r.Attribute(“ Name”)==“ John”)选择新的{键= r.Element( “部门”)。属性(“名称”)。值,值=(从(r.Element(“部门”).Elements(“分支”))中的类型选择type.Value).ToArray()}) .ToDictionary(t => t.key,t => t.value);
只有一条记录要来
Law -- New York, Florida
缺少:-
Lecture -- London, Brit
答案 0 :(得分:0)
您可以先选择所有Department元素:
var employee = (from r in document.Descendants("Detail").Where(r => (string)r.Attribute("Name") == "John").SelectMany(x => x.Elements("Department"))
select new { key = r.Attribute("Name").Value,
value = (from type in r.Elements("Branch") select type.Value).ToArray() })
.ToDictionary(t => t.key, t => t.value);