C# 从多个元素中获取属性值,这些元素具有相同的名称和xml中另一个元素的属性值
我有一个xml文件,如下所示:C# 从多个元素中获取属性值,这些元素具有相同的名称和xml中另一个元素的属性值,c#,xml,linq,C#,Xml,Linq,我有一个xml文件,如下所示: <events> <event id="12345"> <option href="1"></option> <option href="2"></option> <option href="3"></option> <option href="4"></option>
<events>
<event id="12345">
<option href="1"></option>
<option href="2"></option>
<option href="3"></option>
<option href="4"></option>
</event>
</events>
不幸的是,这会在我的foreach循环中为选项生成以下内容:item.options=“System.Xml.Linq.Extensions+d_8”
我想要的是:123451234(是的,我不介意4个选项元素的属性值是否在一个字符串中。我也不能更改xml文件,我更喜欢只使用linq
var nodeWithOptions = from n in xml.Descendants("event")
select new
{
id = (string)n.Attribute("id"),
options = n.Elements("option")
.Select(x => (string)x.Attribute("href"))
.ToList(),
};
这将在给定的事件
元素下的选项
元素上列出href
属性值
var nodeWithOptions = from n in xml.Descendants("event")
select new
{
id = (string)n.Attribute("id"),
options = n.Elements("option")
.Select(x => (string)x.Attribute("href"))
.ToList(),
};