tags:

views:

909

answers:

5

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>

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

the keys are unknown.

how can i do this?

+4  A: 

This looks like a job for 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();
    }
John Nolan
assuming 2008 yes...
annakata
+1  A: 

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?

ChrisAD
And what do you do with the transform output? You still have to get that into a dictionary.
annakata
You can output it however you want. HTML, TXT, up to you. Right?
ChrisAD
but no form of XSLT output implements IDictionary
annakata
oh I didnt know there was a special dictionary implementation. =)
ChrisAD
@anakata It is trivial to generate C# (or any other language) source code with XSLT. So, one could generate the code that defines the dictionary and adds the key-value pairs to it. ChrisAD is correct that the best tool that is especially designed for tree-transformations is XSLT.
Dimitre Novatchev
+3  A: 

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);
            }
gk
+1 xpath loop is the 2.0 solution
annakata
Ah, great minds think alike. My version's quite a bit more concise, but the approach is the same.
Robert Rossney
+2  A: 

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

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

Use LINQ:

Load the document XDocument.Load or XDocument.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 
               };
Michael Damatov