加载XML数据(键/值对)到数据结构 [英] Load XML Data (Key/Value Pairs) into Data Structure
问题描述
我有一个XML数据源,其中包含键/值对的列表。我在寻找一个简单的方法,以相同的数据加载到一个数组或其他一些数据结构,使我可以轻松地查找数据。我可以把它绑定到一对夫妇的点击一个GridView但我没有找到一个简单的方法将其加载到的东西是不是UI控件。
I have an XML Data Source which contains a list of key/value pairs. I'm looking for a simple way to load the same data into an array or some other data structure so that I can easily look up the data. I can bind it to a GridView with a couple of clicks but I'm failing to find a straightforward way to load it into something that isn't a UI Control.
我的数据源是这样的:
<SiteMap>
<Sections>
<Section Folder="TradeVolumes" TabIndex="1" />
<Section Folder="TradeBreaks" TabIndex="2" />
</Sections>
</SiteMap>
我想加载键值对(文件夹,TabIndex的)
I'm wanting to load key value pairs (Folder, TabIndex)
什么是加载数据的最佳方式?
What is the best way to load the data?
推荐答案
使用LINQ to XML:
Using Linq to 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));
}
您得到一本字典,这基本上是键/值对的集合
You get a dictionary, which is basically a collection of key/value pairs
这篇关于加载XML数据(键/值对)到数据结构的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!