我有这个xml块:
<difficulties>
<difficulty difficultyId="1">
<parameter key="maxNumerator" value="10"></parameter>
<parameter key="maxDenominator" value="15"></parameter>
<parameter key="couple" value="2"></parameter>
</difficulty>
<difficulty difficultyId="2">
<parameter key="maxNumerator" value="20"></parameter>
<parameter key="maxDenominator" value="30"></parameter>
<parameter key="couple" value="2"></parameter>
</difficulty>
...
</difficulties>
我想做的是获取类似于:
的模式difficultyId, key, value
像:
1, maxNumerator, 10
1, maxDenominator, 10
1, couple, 2
2, maxNumerator, 20
...
是否有某种方法可以将其插入元组列表中(假设我已经将xml文档加载到xDocument中)。我想使用LINQ:
List<Tuple<string, string, string>> res = from a in xDocument.Descendants("difficulties")
...?
答案 0 :(得分:3)
您可以执行SelectMany
:
var res =(from a in xDocument.Descendants("difficulty")
from parameter in a.Elements("parameter")
select new Tuple<string,string,string>(a.Attribute("difficultyId").Value,
parameter.Attribute("key").Value,
parameter.Attribute("value").Value)).ToList();