getting specific xml data using c# -
i have xml file:
<address> <data2> <person> <empl_num>>100</empl_num> <name>carl</name> <id_num>1</id_num> <isrequired>0</isrequired> </person> <person> <empl_num>200</empl_num> <name>mark</name> <id_num>2</id_num> <isrequired>0</isrequired> </person> <person> <empl_num>300</empl_num> <name>tanner</name> <id_num>3</id_num> <isrequired>0</isrequired> </person> </data2> </address>
i have textbox , button. when type "1" , press button problem how can display xml data datagridview value of textbox show data has id_num = textbox.text
expected output datagrid:
if txtbox1.text = 1: empl_num | name | id_num | isrequired 100 | carl | 1 | 0 if txtbox1.text = 3: empl_num | name | id_num | isrequired 300 | tanner | 3 | 0
try this:
class program { static void main(string[] args) { // read xml somewhere var xml = file.readalltext("address.xml"); xdocument xmldoc = xdocument.parse(xml); // element id var element = getelementbyid(xmldoc, 1); // deserialize element var xmlserializer = new xmlserializer(typeof(person)); var person = (person)xmlserializer.deserialize(element.createreader()); // continue work person } private static xelement getelementbyid(xdocument xmldoc, string id) { // elements according xml file var element = xmldoc.element("address") .elements("data2") .elements("person") .single(x => x.element("id_num").value == id); return element; } } /// <summary> /// used deserialization /// </summary> public class person { public int empl_num { get; set; } public string name { get; set; } public int id_num { get; set; } public bool isrequired { get; set; } }
Comments
Post a Comment