list - C# dictionary keeps erasing values after every iteration -


so have problem... trying consolidate 5 or bilingual dictionaries, in html format, single, multilingual dictionary, english source language. this, decided set dictionary, , map each non-english word it's english counterpart(key) [see code below].

1 public void consolidatedictionary(string directorypath) 2 { 3    directoryinfo directory = new directoryinfo(directorypath); 4    string key = string.empty; 5    string value = string.empty; 6    dictionary<string, list<string>> languages =           new dictionary<string, list<string>>(); 7    list<string> temp = new list<string>(); 8    foreach (fileinfo file in directory.enumeratefiles()) 9    { 10       htmldocument doc = new htmldocument(); 11       doc.load(file.fullname); 12 13       foreach (htmlnode node in doc.documentnode.selectnodes(".//wordunit")) 14       { 15          foreach (htmlnode child in node.selectnodes(".//word")) 16           { 17                   if (child.attributes["language"].value == "en") 18                   { 19                       key = child.outerhtml.tostring(); 20                   } 21                   else 22                   { 23                       value = child.outerhtml.tostring(); 24                   } 25           } 26 27           if (key != null && value != null) 28           { 29               if (languages.containskey(key)) 30               { 31                   foreach (var item in languages[key]) 32                   { 33                       temp.add(item); 34                   } 35                   temp.add(value); 36                   languages.remove(key); 37                   languages.add(key, temp); 38                   temp.clear(); 39               } 40               else 41               { 42                       temp.add(value); 43                       languages.add(key, temp); 44                       temp.clear(); 45               } 46           } 47       } 48   } 49   writefile(languages); 50 } 

basically happening is, after each iteration of foreach loop @ line 15, existing dictionary values nulled (but keys remain). so, after first iteration of loop @ line 15, dictionary (called 'languages') contained: key: <word language="en">hello</word> value: <word language="es">hola</word>; when second iteration comes around, value removed dictionary 'languages', leaving only:

key: <word language="en">hello</word> value: null key: <word language="en">goodbye</word> value: <word language="es">chao</word> 

(where goodbye-chao pair passed in key-value pair second iteration).

what causing strange behavior... knowledge i'm not overwriting values in dictionary @ all! have idea i'm going wrong?

temp.add(value);  //languages.add(key, temp); temp.clear(); 

look @ you're doing poor list instance. use new list instance each key.


if (!languages.containskey(key)) {   languages.add(key, new list<string>()) } languages[key].add(value); 

Comments

Popular posts from this blog

c# - SVN Error : "svnadmin: E205000: Too many arguments" -

c# - Copy ObservableCollection to another ObservableCollection -

All overlapping substrings matching a java regex -