So I have a problem… I am trying to consolidate 5 or so bilingual dictionaries, in HTML format, into a single, multilingual dictionary, with english as the source language. To do this, I decided to set up a dictionary, and map each non-english word to 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 what is happening is, after each iteration of the foreach loop at line 15, the existing dictionary values are all nulled (but the keys remain). So, say that after the first iteration of the loop at line 15, the dictionary (called ‘languages’) contained: key: <word language="EN">Hello</word> Value: <word language="ES">Hola</word>; when the second iteration comes around, the value is removed from the 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 the Goodbye-Chao pair were passed in as the key-value pair for the second iteration).
What could be causing this strange behavior… to my knowledge I’m not overwriting the values in my dictionary at all! Does anyone have any idea where I’m going wrong?
Look at what you’re doing to that poor List instance. Use a new List instance for each key.