asp.net - dropdown list bind from xml file -
i having issue binding dropdown data within xml file. xml file looks so;
<agricultural> <file> <text>acreage_cotton_planted</text> <value>acrecotp index</value> </file> <file> <text>acreage_corn_planted</text> <value>acrecrnp index</value> </file> <file> <text>acreage_soybean_planted</text> <value>acresoyp index</value> </file> <file> <text>acreage_wheat_planted</text> <value>acrewhtp index</value> </file> </agricultural>
i using code return list xml
public shared function getagdatalist(nodestring string) list(of listitem) dim doc new xmldocument() 'load xml file xmldocument object doc.load("~\datafiles\dataxml.xml") 'this needs changed server path dim root xmlnode = doc.documentelement 'select nodes tag paramter indicated nodestring variable dim nodelist xmlnodelist = root.selectnodes(nodestring) return (from node xmlnode in nodelist let text = node.attributes.getnameditem("text").value.tostring() let val = node.attributes.getnameditem("value").value.tostring() select new listitem(text, val)).tolist() end function
isn't dropdown list control supposed display text? because dropdown showing text , value concatenated together. example, acreage_corn_plantedacrecrnp index. want text, acreage_corn_planted, displayed.
you can use following code: used linq entity works asp.net 3.5 or above:
using system.xml.linq; public class item { public string text { get; set; } public string value { get; set; } } public class mypage : page { xdocument xdoc = xdocument.load("myxmlfile.xml"); var listofitems = new list<item>(); foreach (xelement item in xdoc.element("agricultural").elements("file")) { listofitems.add(new item() { text = item.element("text").value, value = item.element("value").value }); } mydrp.datatextfield = "text"; mydrp.datavaluefield = "value"; mydrp.datasource = listofitems; mydrp.databind(); }
Comments
Post a Comment