我的xml看起来像这样
<stock name="Fish">
<eqn>100</eqn>
<inflow>Spawns</inflow>
<outflow>Deaths</outflow>
<outflow>Flow_1</outflow>
<non_negative/>
</stock>
我的代码看起来像这样
var stockList = from q in variable.Descendants(ns + "stock")
select new {
name = q.FirstAttribute.Value,
initial = q.Element(ns + "eqn").Value,
inflow = q.Element(ns + "inflow").Value,
outflow = q.Element(ns + "outflow").Value
};
这只给我第一个流入和第一个流出,但是有两个流出,因此我需要将每个流出都存储在一个数组中。
答案 0 :(得分:0)
您可以使用public IEnumerable<XElement> Elements (XName name)
method。它返回该元素具有匹配名称的所有子元素。
outflow = q.Elements(ns + "outflow").Select(el => el.Value).ToArray()
或
outflow = (from el in q.Elements(ns + "outflow") select el.Value).ToArray()