将 xml 转换为排序字典 [英] convert xml to sorted dictionary

查看:27
本文介绍了将 xml 转换为排序字典的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个与此类似的 xml 文件:

i have an xml file similar to this:

<?xml version="1.0" encoding="UTF-8"?>
<data>
    <resource key="123">foo</resource>
    <resource key="456">bar</resource>
    <resource key="789">bar</resource>

</data>

我想把它作为键值对放入字典(排序)中.IE:123:富,456:bar...等

i want to put this into a Dictionary (sorted) as key value pairs. i.e: 123:foo, 456:bar...etc

密钥未知.

我该怎么做?

推荐答案

试试这个,

string s = "<data><resource key=\"123\">foo</resource><resource key=\"456\">bar</resource><resource key=\"789\">bar</resource></data>";
XmlDocument xml = new XmlDocument();
xml.LoadXml(s);
XmlNodeList resources = xml.SelectNodes("data/resource");
SortedDictionary<string,string> dictionary = new SortedDictionary<string,string>();
foreach (XmlNode node in resources){
   dictionary.Add(node.Attributes["key"].Value, node.InnerText);
}

这篇关于将 xml 转换为排序字典的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆