开发者

How to put a node in a dictionary using LINQ to XML

This is the XML file which I have:

<Root>
    <Level1>
        <Foo ID="1" Count="20" />
        <Foo ID="2" C开发者_开发知识库ount="28" />
        <Foo ID="3" Count="25" />
    </Level1>
</Root>

I only have one Level 1 element in my XML, and inside of it there several Foo nodes.

How can I get that Foo nodes in a dictionary?

I mean Dictionary<int, int>.


var doc = XDocument.Load(fileName);
var dictionary =
    doc.Root
        .Element("Level1")
        .Elements("Foo")
        .ToDictionary(
            e => (int)e.Attribute("Id"),
            e => (int)e.Attribute("Count"));


XElement.Parse("XML here").Descendants("Foo").Select( s => s).ToDictionary( x=> x.Attribute("ID").Value, x=> x.Attribute("Count").Value))

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜