2014-03-19 79 views
1

名单解析与多个值比较词典<字符串,列表<string>>单值与值的C#

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

doc.Load(confidencethresholdFilePath + "\\input.xml"); 
// XmlNodeList nodes = doc.DocumentElement.SelectNodes("/root/file"); 
doc.Normalize(); 
XmlNodeList nList = doc.GetElementsByTagName("key"); 
for (int temp = 0; temp < nList.Count; temp++) 
{ 
    string keyvalue = ""; 
    XmlNode nNode = nList.Item(temp); 
    List<String> main = new List<string>(); 
    ArrayList arrayList = new ArrayList(main); 
    XmlElement ele = (XmlElement)nNode; 
    keyvalue = ele.GetAttribute("value"); 
    for (int i = 0; i < ele.ChildNodes.Count; i++) 
    { 
     if (ele.ChildNodes[i].ChildNodes.Count == 1) 
     { 
      Double sec = Double.Parse(ele.ChildNodes[i].InnerText); 
      int seg = TimeToSegment(sec); 
      Console.WriteLine("" + seg); 
      main.Add(Convert.ToString(seg)); 
     } 
    } 
    dictionary.Add(keyvalue, main); 
} 

我想与单个值comapare但同样关键的XML和形式字典它显示错误 dictionary.ContainsValue(s.sname)参数无效

回答

0

我不知道要检查什么,但如果你想匹配的密钥,然后做到这一点:

dictionary.ContainsKey(s.sname); 

或者你可以尝试:

IEnumerable<KeyValuePair<string,List<string>>> ienKeyVals = dictionary.Where(x => x.Value.Contains(s.sname)); 

解析与相同的密钥的XML和形式字典多个值

字典不能包含重复键。无法将重复项添加到词典中 - 另一种方法是使用Lookup类。

Enumerable.ToLookup Method

创建从IEnumerable一个通用的查找。

相关问题