Hello. How to make a replacement in the xml file of the items selected in the listbox by index?

Tried to do so for if you select all the replacement works, if you select several different ones in the listBox it shows an error.

Suppose if I select the United States and the Czech Republic in the listbox then replace the France France in the xml file if other elements are different.

listbox

USA Poland Czech Republic

xml file

<Root> <Statya> <TypeTermins>Россия</TypeTermins> <TypeTermins>Германия</TypeTermins> <TypeTermins>Франция</TypeTermins> </Statya> </Root> 

<cite>

  List<XElement> hmlno = new List<XElement>(); IEnumerable<XElement> list = oldXElement.XPathSelectElements("TypeTermins"); System.Collections.ArrayList selectedd = new System.Collections.ArrayList(listBoxAlfa.SelectedItems); foreach (XElement elll in list) { List<XElement> listAgain = list.ToList(); XElement[] arv = list.ToArray(); hmlno.Add(elll); if (listBoxAlfa.Items.Count != 0) { if (listBoxAlfa.Items.Count == 1 && listBoxAlfa.SelectedIndex == 0 && listBoxAlfa.SelectedItems.Count == 1) { hmlno[0].Value = listBoxAlfa.SelectedItems[listBoxAlfa.SelectedIndex].ToString(); listAgain[0].Value = hmlno[0].Value; } else if (listBoxAlfa.Items.Count > 1 && listBoxAlfa.SelectedIndex == 0 && listBoxAlfa.SelectedItems.Count == 1) { hmlno[0].Value = listBoxAlfa.SelectedItems[listBoxAlfa.SelectedIndex].ToString(); listAgain[listBoxAlfa.SelectedIndex].Value = hmlno[0].Value; } else if (listBoxAlfa.Items.Count > 1 && listBoxAlfa.SelectedIndex == 1 && listBoxAlfa.SelectedItems.Count == 1) { for (int i = 1; i < hmlno.Count; i++) { if (listBoxAlfa.SelectedIndex == i) { hmlno[i].Value = selectedd[listBoxAlfa.SelectedIndex - 1].ToString(); listAgain[i].Value = hmlno[i].Value; } } } else if (listBoxAlfa.Items.Count > 1 && listBoxAlfa.Items.Count == listAgain.Count && listBoxAlfa.SelectedItems.Count == 1 && listBoxAlfa.SelectedIndex >= 0) { try { for (int i = 0; i < hmlno.Count; i++) { hmlno[i].Value = selectedd[listBoxAlfa.SelectedIndex].ToString(); listAgain[listBoxAlfa.SelectedIndex].Value = hmlno[i].Value; } } catch (Exception exc) { if (listBoxAlfa.SelectedIndex == listBoxAlfa.Items.Count) { } else if (listBoxAlfa.SelectedIndex < listBoxAlfa.Items.Count) { for (int i = 0; i < hmlno.Count; i++) { if (listBoxAlfa.SelectedIndex == i) { hmlno[i].Value = selectedd[0].ToString(); listAgain[listBoxAlfa.SelectedIndex].Value = hmlno[i].Value; } } } } } else if (listBoxAlfa.Items.Count > 1 && listBoxAlfa.SelectedItems.Count > 1) { foreach (Object selectedItemm in selectedd) { for (int i = 1; i < hmlno.Count; i++) { for (int pl = 0; pl < arv.Length; pl++) { if (arv.Length > selectedd.Count) { if (pl == 1) { arv[pl - 1].Value = listBoxAlfa.SelectedItems[pl - 1].ToString(); listAgain[pl - 1].Value = arv[pl - 1].Value; } else if (pl == 2) { arv[pl].Value = listBoxAlfa.SelectedItems[pl - 1].ToString(); listAgain[pl].Value = arv[pl].Value; } } else if (arv.Length <= selectedd.Count) { arv[pl].Value = listBoxAlfa.SelectedItems[pl].ToString(); listAgain[pl].Value = arv[pl].Value; } } } } } } } 

</ cite>

  • In my opinion, everything should be much simpler, and in essence hardly anyone will understand this code - semenvx27
  • Not such a big code, just kind of unreadable here. @Demon, try to fix it, then at least we can read and understand it. - Maxim Kamalov 2:44 pm
  • How easy is it because I can select any number of elements and the element can have any selectedIndex? Suppose if I select 1 item in the listbox and 3 then it changes with all the other items or the change does not occur. - Demon

0