【发布时间】:2025-11-23 16:25:01
【问题描述】:
我有一个如下所示的 xml 文件:
<events>
<event id="12345">
<option href="1"></option>
<option href="2"></option>
<option href="3"></option>
<option href="4"></option>
</event>
</events>
我正在尝试从这些节点中选择一对信息:事件 id (12345) 和元素选项的属性
var nodeWithOptions = from n in xml.Descendants("event")
select new
{
id = n.Attribute("id").Value,
options = n.Elements("option").Attributes("href").ToString(),
};
不幸的是,这会在我的 foreach 循环中生成以下选项:item.options = "System.Xml.Linq.Extensions+d__8"
我想要的是:12345、1234(是的,我不介意 4 个选项元素的属性值是否在一个字符串中。而且我也无法更改 xml 文件,我宁愿只使用 linq。
【问题讨论】: