c# - Using Linq to get XML data from database -
i'm struggling values of single xml node using linq.
here xml.
<?xml version="1.0" encoding="utf-8"?> <record> <addressline1>abcd street</addressline1> <addressline2>xyz ave</addressline2> <addresscity>illinois</addresscity> <addressstate>chicago</addressstate> <addresszip>23434</addresszip> </record>
and here c# code
xelement xmldoc = xelement.parse(varq.content); //when debug find xmldoc contains xml. alright. var q = (from lpi in xmldoc.descendants("record") select new { addressline1 = lpi.element("addressline1").value, addressline2 = lpi.element("addressline2").value, addresscity = lpi.element("addresscity").value, addresscountry = lpi.element("addresscountry").value, addressstate = lpi.element("addressstate").value, addresszip = lpi.element("addresszip").value, }).firstordefault();
var q shows null. can u please me find out wrong here?
replace descendants on descendantsandself:
string xml = @"<?xml version=""1.0"" encoding=""utf-8""?> <record> <addressline1>abcd street</addressline1> <addressline2>xyz ave</addressline2> <addresscity>illinois</addresscity> <addressstate>chicago</addressstate> <addresszip>23434</addresszip> </record>"; xelement xmldoc = xelement.parse(xml); var q = (from lpi in xmldoc.descendantsandself("record") select new { addressline1 = (string)lpi.element("addressline1"), addressline2 = (string)lpi.element("addressline2"), addresscity = (string)lpi.element("addresscity"), addresscountry = (string)lpi.element("addresscountry"), addressstate = (string)lpi.element("addressstate"), addresszip = (string)lpi.element("addresszip"), }).firstordefault(); console.writeline(q);
print:
{ addressline1 = abcd street, addressline2 = xyz ave, addresscity = illinois, addresscountry = , addressstate = chicago, addresszip = 23434 }
Comments
Post a Comment