c# - Loading Xml into DropDown -


my xml file looks this.

<?xml version="1.0" encoding="utf-8" ?> <test1>   <product name="test1" value="10">     <catagory>       <catagorytype value="20">aaa</catagorytype>       <catagorytype value="22">bbb</catagorytype>       <catagorytype value="23">ccc</catagorytype>       <catagorytype value="25">ddd</catagorytype>     </catagory>     <type>       <typename value="11">111</typename>       <typename value="12">222</typename>       <typename value="13">333</typename>       <typename value="14">444</typename>     </type>     <location>       <area value="0">inside</area>       <area value="1">outside</area>       <area value="2">nowhere</area>     </location>   </product>   <product name="test2" value="10">     <catagory>       <catagorytype value="20">eee</catagorytype>       <catagorytype value="22">fff</catagorytype>       <catagorytype value="23">ggg</catagorytype>       <catagorytype value="25">hhh</catagorytype>     </catagory>     <type>       <typename value="11">555</typename>       <typename value="12">666</typename>       <typename value="13">777</typename>       <typename value="14">888</typename>     </type>     <location>       <area value="0">inside</area>       <area value="1">outside</area>       <area value="2">nowhere</area>     </location>   </product> </test1> 

i trying make can select product in drop down. based on product, category in dropdown. type in another, , location in last. have been able products show in drop down. not able rest of elements in based on product.

my code load products drop down:

xmldataset.readxml(server.mappath("xmlfile1.xml"));  drpproducts.datasource = xmldataset.tables["product"];  drpproducts.datatextfield = "name"; drpproducts.datavaluefield = "value"; drpproducts.databind(); 

i cannot figure next steps. how go doing it?

here solution, using linq xml. idea presented in following steps:

  1. first, load product product dropdown.
  2. when product dropdown changed, new value , populate appropriate data rest dropdown.

here code. create demo program using windows application. it's not important, focus on how select , populate data.

first, declaration:

private xdocument _document; 

run function when first load:

private void loaddata() {     // load document     _document = xdocument.load("product.xml");      // root's children, here products     ienumerable<xelement> products = _document.root.elements();      // product name     var productnames = new list<string>();     foreach (var element in products)     {         productnames.add(element.attribute("name").value);     }      // assign source     ddlproduct.datasource = productnames; } 

when product name change, load appropriate category.

private void ddlproduct_selectedindexchanged(object sender, eventargs e) {     // new value     string productname = ddlproduct.text;      // ignore empty value, or xpath wrong     if (string.isnullorempty(productname))     {         return;     }      // build xpath     string xpath = string.format("//product[@name='{0}']", productname);      // select product name     xelement product = _document.xpathselectelement(xpath);      // select categorytype     ienumerable<xelement> categories = product.descendants("catagorytype");      // name     var categorynames = new list<string>();     foreach (var category in categories)     {         categorynames.add(category.value);     }      // assign source     ddlcategory.datasource = categorynames; } 

to make answer simple, populate category. others same.

if want know more xpath syntax, @ link. , more information linq xml, check tutorial.


Comments

Popular posts from this blog

java - Date formats difference between yyyy-MM-dd'T'HH:mm:ss and yyyy-MM-dd'T'HH:mm:ssXXX -

c# - Get rid of xmlns attribute when adding node to existing xml -