簡體   English   中英

如何使用LINQ to XML將節點放入字典中

[英]How to put a node in a dictionary using LINQ to XML

這是我擁有的XML文件:

<Root>
    <Level1>
        <Foo ID="1" Count="20" />
        <Foo ID="2" Count="28" />
        <Foo ID="3" Count="25" />
    </Level1>
</Root>

我的XML中只有一個1級元素,里面有幾個Foo節點。

如何在字典中獲取Foo節點?
我的意思是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))

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM