Reading XMl Data same nodes in C# -
i have got following xml data.
<useraddress> <addr> <streetaddress/> <streetaddress>some street</streetaddress> <streetaddress>town</streetaddress> <streetaddress>city</streetaddress> <streetaddress>london</streetaddress> <postcode>l45 1sg</postcode> </addr> </useraddress>
i trying read each of street address line , store value in variable following code.
xelement elem = xelement.parse(xmldata); var address = elem.descendants("addr").tolist(); foreach (var adr in address) { current_user.address_1 = addr.element("streetaddress").value.tostring(); current_user.address_2 = addr.element("streetaddress").value.tostring(); current_patient.address_3 = addr.element("streetaddress").value.tostring(); current_patient.address_4 = addr.element("streetaddress").value.tostring(); current_patient.address_5 = addr.element("streetaddress").value.tostring(); current_patient.post_code = addr.element("postcode").value; }
the problem facing above code is assigning first line value of streetaddress variables address_1, address_2, address_3, address_4, address_5.
if want list of addresses can elements directly. have assign values based on criteria
xelement elem = xelement.parse(xmldata); ienumerable<xelement> addresses = elem.descendants("streetaddress");
now can loop through , assign values.
Comments
Post a Comment