【发布时间】:2014-01-24 16:45:33
【问题描述】:
我有一个类似于以下的 xml 文件:
<doc>
<file>
<header>
<source>
RNG
</source>
</header>
<body>
<item name="items.names.id1">
<property>propertyvalue1</property>
</item>
<!-- etc -->
<item name="items.names.id100">
<property>propertyvalue100</property>
</item>
<!-- etc -->
<item name="otheritems.names.id100">
<property>propertyvalue100</property>
</item>
</body>
</file>
</doc>
还有以下类:
private class Item
{
public string Id;
public string Property;
}
例如,该文件有 100 个项目条目(在名称属性中标记为 1 到 100)。如何使用 Linq Xml 获取这些节点并将它们放入项目列表中?
使用 Selman22 的示例,我正在执行以下操作:
var myList = xDoc.Descendants("item")
.Where(x => x.Attributes("name").ToString().StartsWith("items.names.id"))
.Select(item => new Item
{
Id = (string)item.Attribute("name"),
Name = (string)item.Element("property")
}).ToList();
但是,列表是空的。我在这里错过了什么?
【问题讨论】:
-
您发布的“XML”无效。另外,你试过什么?