gpt4 book ai didi

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

转载 作者:数据小太阳 更新时间:2023-10-29 02:48:49 25 4
gpt4 key购买 nike

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

我的数据源是这样的:

<SiteMap>
<Sections>
<Section Folder="TradeVolumes" TabIndex="1" />
<Section Folder="TradeBreaks" TabIndex="2" />
</Sections>
</SiteMap>

我想加载键值对(文件夹,TabIndex)

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

最佳答案

使用 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));
}

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

关于c# - 将 XML 数据(键/值对)加载到数据结构中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1477075/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com