将XML数据(键/值对)加载到数据结构中

mac*_*ojw 2 c# xml asp.net xmldatasource

我有一个XML数据源,其中包含键/值对列表.我正在寻找一种将相同数据加载到数组或其他数据结构中的简单方法,以便我可以轻松查找数据.我可以通过几次点击将它绑定到GridView,但我没有找到一种直接的方法将其加载到不是UI控件的东西.

我的数据源如下:

<SiteMap>
  <Sections>  
    <Section Folder="TradeVolumes" TabIndex="1" />
    <Section Folder="TradeBreaks" TabIndex="2" />
  </Sections>
</SiteMap>
Run Code Online (Sandbox Code Playgroud)

我想加载键值对(Folder,TabIndex)

加载数据的最佳方法是什么?

Tho*_*que 8

使用Linq到XML:

var doc = XDocument.Parse(xmlAsString);
var dict = new Dictionary<string, int>();
foreach (var section in doc.Root.Element("Sections").Elements("Section"))
{
    dict.Add(section.Attribute("Folder").Value, int.Parse(section.Attribute("TabIndex").Value));
}
Run Code Online (Sandbox Code Playgroud)

你得到一个字典,它基本上是键/值对的集合