c# - Populate ComboBox from XML -


i'm trying populate combobox in c# using field xml file, no luck... don't know wrong here (it doesn't show anything):

private void combobox1_selectedindexchanged(object sender, eventargs e)     {         xmldocument doc = new xmldocument();         doc.load("baza_de_cunostinte.xml");          var datasource = new list<persoane>();         string persoanaplacuta;          foreach (xmlnode node in doc.documentelement)         {             string persoanaplacuta = node["persoanaplacuta"].innertext.replace("\"", "");              combobox1.items.add(persoanaplacuta);         }      } 

this xml file:

 <root>   <persoane>     <nume>"bob"</nume>     <ismale>true</ismale>     <varsta>30</varsta>     <persoanaplacuta>"iulia"</persoanaplacuta>   </persoane>    <persoane>     <nume>"bogdan"</nume>     <ismale>true</ismale>     <varsta>28</varsta>     <persoanaplacuta>"ana"</persoanaplacuta>   </persoane>  </root> 

i don't think searching in right xml node address. should full address in order find targeted node.

use xmlnodelist nodes full address , loop through items:

private void combobox1_selectedindexchanged(object sender, eventargs e) {     xmldocument doc = new xmldocument();     doc.load("baza_de_cunostinte.xml");      var datasource = new list<persoane>();     string persoanaplacuta;        xmlnodelist xmlnodelist = doc.selectnodes("//root//persoane");      foreach (xmlnode node in xmlnodelist)      {         string persoanaplacuta = node.childnodes[3].innertext.replace("\"", "");         combobox1.items.add(persoanaplacuta);      } } 

also can change foreach loop this:

foreach (string persoanaplacuta in xmlnode node in xmlnodelist                              select node.childnodes[3].innertext.replace("\"", "")) {     combobox1.items.add(persoanaplacuta); } 

note: better add combobox1.items.clear(); @ first line, otherwise repetitive items in combobox


Comments

Popular posts from this blog

c - How to retrieve a variable from the Apache configuration inside the module? -

c# - Constructor arguments cannot be passed for interface mocks -

python - malformed header from script index.py Bad header -