代码之家  ›  专栏  ›  技术社区  ›  Thorin Oakenshield

在C语言中向听写添加数据#

  •  2
  • Thorin Oakenshield  · 技术社区  · 14 年前

    我有一个XML文件

     <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>
    

    和一个独裁者

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

    我需要将文件中的数据添加到dictionary中,比如:

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

    现在我用的是

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

    但它引发了一个异常,例如“与”System.Collections.Generic.List.List(int)“匹配的最佳重载方法具有一些无效参数”

    请给我一个更好的解决办法

    提前谢谢

    2 回复  |  直到 14 年前
        1
  •  3
  •   Benny    14 年前

    如果出于任何原因,你想用经典的方式来做(我已经建立了它,而乔恩发布了他的,所以不妨发布它),这里是你如何做到的。

    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());
    
        2
  •  3
  •   Jon Skeet    14 年前

    Linq有内置的支持 查找 :

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

    请注意,这将返回 ILookup<string, string> -这不是一个 Dictionary ,但它允许您按键查找组,如下所示:

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

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

    如果你 真正地 想要一个 Dictionary<string, List<string>> 你可以用这个:

    var dictionary = lookup.ToDictionary(group => group.Key,
                                         group => group.ToList());