Read xml file and write ids' values into related textbox in C# -
i have xml file named "numbers.xml" this:
<?xml version="1.0" encoding="utf-8" ?> <program> <box id="aaa" value="78678"/> <box id="bbb" value="37287"/> <box id="ccc" value="783"/> <box id="ddd" value="7867"/> <box id="eee" value="786"/> <box id="fff" value="23"/> <box id="ggg" value="453"/> <box id="hhh" value="4537"/> </program>
i want read xml file , fill textboxes. in windows forms application txtaaa.text
value must take id="aaa" value 78678. likewise txtbbb.text
value must take id="bbb" value 37287. how can this?
edit:
i tried this:
xmldocument xmldoc = new xmldocument(); xmldoc.load(openfiledialog1.filename); xmlnodelist nodelist = xmldoc.documentelement.childnodes; xmlnode xmlnode = nodelist.item(0); txtaaa.text = xmlnode.attributes["id"].innertext;
but "aaa" shown in textbox. totally failure. –
you create list of xml items , assign based on textbox name in foreach loop
assuming textbox names are:
txtaaa txtbbb txtccc ...etc
you can remove txt
part find correct value id
var data = xelement.load("c:\\test.xml").descendants("box"); foreach (var textbox in controls.oftype<textbox>()) { var value = data.firstordefault(v => v.attribute("id").value == textbox.name.replace("txt","").tolower()); if (value != null) { textbox.text = value.attribute("value").value; } }
test: