我有一个 xml,我试图通过 LINQ 查询从中提取一些信息。
xml 文件的格式如下所示:
<TopNode>
<Sample name="Tom" id="0" batch="1">
<Sequences>
<NextItem id="10">Stand1 ></NextItem>
<PreviousItem id="9">Stand2 ></PreviousItem>
</Sequences>
</Sample>
<Sample name="Hill" id="1" batch="1">
<Sequences>
<NextItem id="1">Stand1 ></NextItem>
<NextItem id="2">Stand3 ></NextItem>
<PreviousItem id="3">Stand4 ></PreviousItem>
</Sequences>
</Sample>
.
.
.
</TopNode>
我必须获得两者的属性列表NextItem
and PreviousItem
从查询中。为此,我定义了一个类来获取结果。
public class Extract
{
public string name
{
get;
set;
}
public int _id
{
get;
set;
}
}
LINQ查询(对于每个Sample节点,获取类对象中的PreviousItem和NextItem内容):
var EnumerableContent = from item in XElement.Load("file.xml").Elements("NextItem")
select ???...
问题是如何将数据传递到上面查询中每个 Sample 节点的类对象。
。
其次,上面只会得到 NextItem 节点。如何编写 NextItem 和 PreviousItem 的查询?
EDIT总的来说,我必须为每个得到一个 IEnumerableSequence
节点,然后从查询返回整个 IEnumerable。
EDIT2分配key
这是给我这个错误。查询如下
var mapping = XDocument.Load("file.xml")
.Descendants("Sequences")
.Select(n => n.Descendants("PreviousItem")
.Union(n.Descendants("NextItem"))
.Select(n1 => new Extract { _id = (int)n1.Attribute("id"), Name = n1.Value })
).ToDictionary<IEnumberable<Menu>, int>(key => key._id);//and further converting this dictionary to sorted dictionary (sorted accordign to keys)