2

I've KeyValuePair in xml like this:

<list_pair key="WorkMode">
    <str_pair key="1" value="&amp;1 Ready" />
    <str_pair key="2" value="&amp;2 Not ready" />
</list_pair>

I'm able to add them into a dictionary:

foreach (XmlNode wmNode in wmNodeList)
    wmDictionary.Add(wmNode.Attributes["key"].Value, wmNode.Attributes["value"].Value);

Since I want the value to be displayed nicely in a menu in my application interface, I want to change the value to be displayed. For example, I want to change the value "& 1 Ready" to "1 Ready". So, how can I do that changes? Here's what I've done so far:

foreach(var key in wmDictionary.Keys)
{
    switch (key)
    {
        //How to do the changes?
    }
}

Please help.

dario
  • 5,149
  • 12
  • 28
  • 32
YWah
  • 571
  • 13
  • 38
  • wmDictionary[key] = "Your new value"; – Kram Jun 28 '15 at 12:30
  • Within the loop I imagine you would do: `wmDictionary[key] = someNewValue;` but it's not clear at all what that `switch` statement is meant to do. – David Jun 28 '15 at 12:31

7 Answers7

2

If it's just the ampersands you want to remove, you can put it as part of your LINQ query.

foreach (XmlNode wmNode in wmNodeList)
    wmDictionary.Add(wmNode.Attributes["key"].Value, 
                     wmNode.Attributes["value"].Value.ToString().Replace("&", "").Trim());
keyboardP
  • 68,824
  • 13
  • 156
  • 205
1

Why not simply use string.Replace or string.TrimStart when populating the dictionary?

wmNode.Attributes["value"].Value.Replace("&amp;", "");
Zohar Peled
  • 79,642
  • 10
  • 69
  • 121
1

First, you shouldn't be switching on key when you wish tomodify value. Moreover, you shouldn't be switching at all: a better way is to set up a map with translations, and use it to look up translated values, like this:

// This dictionary can be defined on the class
// as a private static readonly member.
var translations = new Dictionary<string,string> {
    {"original1", ""translation1}
,   {"original2", "translation2"}
};
foreach(var kvp in wmDictionary) {
    string translated;
    if(!translations.TryGetValue(kvp.Value, out translated)){
        translated=kvp.Value;
    }
    Console.WriteLine("Key={0} Translated value={1}", kvp.Key, translated);
}
Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
  • Hi @dasblinkenlight, thanks for your suggestion, since yours is more customizable, i accepted your answer – YWah Jun 29 '15 at 00:57
  • Hi @dasblinkenlight...can I ask you one more thing? how to write the else statement if the translation value is not found, then the translated string equals to the original value? – YWah Jun 29 '15 at 01:09
  • @YWah That's what the `if` statement does: if `TryGetValue` succeeds, `translated` gets assigned as part of the method call. Otherwise, `translated=kvp.Value;` is performed. – Sergey Kalinichenko Jun 29 '15 at 01:19
1
var dic = new Dictionary<string, string>();    
dic.Keys.ToList().ForEach(dd => dic[dd] = dic[dd].Replace("&amp;", ""));
farid bekran
  • 2,684
  • 2
  • 16
  • 29
0

Why would you need switch? Do like following -

foreach(var key in wmDictionary.Keys)
{
    wmDictionary[key] = "do whatever operation you want to do";
}
NBM21
  • 47
  • 1
  • 8
0

So what i understood was you want to remove your "&" from menu options, here is the code example you can use, i just typed it into notepad and paste it over here to give you an idea,

    foreach(var key in wmDictionary.Keys)
 {
   switch (key)
   {
     string originalVal = wmDictionary[key].Value;
     string newVal = originalVal.replace("&","");
     wmDictionary[key] = newVal
   }
 }
ATHER
  • 3,254
  • 5
  • 40
  • 63
0

Don't modify collections in foreach read this and this

Do something like this:

private string ModifyKey(string key){
    return key.Replace("&amp;","");
}

And modify it before adding it to the dictionary

foreach (XmlNode wmNode in wmNodeList){
    wmDictionary.Add(ModifyKey(wmNode.Attributes["key"].Value), wmNode.Attributes["value"].Value);
}
Community
  • 1
  • 1
Bobby Tables
  • 2,953
  • 7
  • 29
  • 53