tags:

views:

85

answers:

2

I've an xml file like

 <Data> 
  <Element ValOne="1" Key="KeyOne" /> 
  <Element ValOne="2" Key="KeyOne" /> 
  <Element ValOne="3" Key="KeyOne" /> 
  <Element ValOne="4" Key="KeyOne" /> 
  <Element ValOne="5" Key="KeyTwo" /> 
  <Element ValOne="6" Key="KeyTwo" /> 
  <Element ValOne="7" Key="KeyThree" /> 
  <Element ValOne="8" Key="KeyThree" /> 
  <Element ValOne="9" Key="KeyThree" /> 
  <Element ValOne="10" Key="KeyThree" /> 
 </Data>

and a dictonary

 Dictionary<string, List<string>> m_dictSample = new Dictionary<string, List<string>>();

i need to add the data from the file to dictonary like:

KeyOne       "1"
             "2"
             "3"
             "4"
KeyTwo       "5"
             "6"
KeyThree     "7"
             "8"
             "9"
            "10"

now i'm using like

List<string> lst = new List<string>();


lst = (XDocument.Load(Application.StartupPath + "\\Sample.xml").Descendants("Element").
            Select(l_Temp => l_Temp.Attribute("Key").Value)).ToList();



 m_dictSample = (from str in lst
                        from el in XDOC.Descendants("Element")
                        where (str == el.Attribute("Key").Value)
                        select new { Key = str, value =new List<string>( el.Attribute("ValOne") }).
                      ToDictionary(l_Temp => l_Temp.Key, l_Temp => l_Temp.value);

but its throwing an exception like " The best overloaded method match for 'System.Collections.Generic.List.List(int)' has some invalid arguments

please give me a better solution

Thanks in advance

+3  A: 

LINQ has built-in support for this with lookups:

m_dictSample = doc.Descendants("Element")
                  .ToLookup(element => (string) el.Attribute("Key"),
                            element => (string) el.Attribute("ValOne"));

Note that this will return an ILookup<string, string> - this isn't a Dictionary, but it allows you to look up a group by key, like this:

foreach (string value in lookup["some key"])
{
    Console.WriteLine(value);
}

or

foreach (var group in lookup)
{
    Console.WriteLine(group.Key);
    foreach (string value in group)
    {
        Console.WriteLine("  " + value);
    }
}

If you really want a Dictionary<string, List<string>> you could use this:

var dictionary = lookup.ToDictionary(group => group.Key,
                                     group => group.ToList());
Jon Skeet
+2  A: 

If for any reason you want to do it the classic way (I already built it while Jon posted his so might as well post it) here is how you could do it

XDocument document = XDocument.Load(Application.StartupPath + "\\Sample.xml");

Dictionary<string, List<string>> result = (from root in document.Descendants("Element")
                                                       group root by root.Attribute("Key").Value into KeyGroup
                                                       select KeyGroup)
                                                       .ToDictionary(grp => grp.Key, 
                                                                     grp => grp.Attributes("ValOne").Select(at => at.Value).ToList());
Benny