C# 将 xml 转换为排序字典

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/653540/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-04 11:59:11  来源:igfitidea点击:

convert xml to sorted dictionary

c#xml

提问by raklos

i have an xml file similar to this:

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

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

</data>

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

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

the keys are unknown.

钥匙是未知的。

how can i do this?

我怎样才能做到这一点?

采纳答案by gk.

Try this,

尝试这个,

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

回答by Johnno Nolan

This looks like a job for Linq to Xml

这看起来像是 Linq to Xml 的工作

    static void Main(string[] args)
    {            
        XDocument yourDoc = XDocument.Load("the.xml");
        var q = from c in yourDoc.Descendants("resource")
                orderby (int) c.Attribute("key")
                select c.Attribute("key").Value + ":" + c.Value;

        foreach (string s in q)
            Console.WriteLine(s);                            
        Console.ReadLine();
    }

回答by Chris Dale

I would do this with XSLT transformation. Do you need to do the job with C#? If not you can simply make a XSLT document which parses through all resource tags and outputs key:value sorted out for you. Very easy accomplished. Is this a solution you want?

我会用 XSLT 转换来做到这一点。你需要用 C# 来完成这项工作吗?如果没有,您可以简单地制作一个 XSLT 文档,该文档解析所有资源标签并输出为您整理的键:值。很容易实现。这是您想要的解决方案吗?

回答by Robert Rossney

This is actually easier without using Linq and just using an XmlDocument:

这实际上更容易不使用 Linq 而只使用一个XmlDocument

SortedDictionary<string, string> myDict = new SortedDictionary<string, string>();
foreach (XmlElement e in myXmlDocument.SelectNodes("/data/resource"))
{
   myDict.Add(e.GetAttribute("key"), e.Value);
}

回答by Michael Damatov

Use LINQ:

使用 LINQ:

Load the document XDocument.Loador XDocument.Parse:

装入文件XDocument.LoadXDocument.Parse

var xml = XDocument.Load(...);

Iterate through the ordered sequence:

遍历有序序列:

var sequence = from e in xml.Root.Elements() 
               let key = (string)e.Attribute("key")
               order by key
               select new { 
                 Key = key, 
                 Value = (string)e 
               };