i know this is a simple question , so apologies , but i’m new to linq to xml , and am having difficulty with the following :
I have the following XML and am trying to get out all the elements , including the Address elements :
<LawCentres>
<LawCentre>
<LawCentreID>2</LawCentreID>
<LawCentreName>Test Law Centre</LawCentreName>
<Address>
<Address1>Test Address1</Address1>
<Address2>Test Address2</Address2>
<Address3>Test Address3</Address3>
</Address>
</LawCentre>
<LawCentre>
<LawCentreID>22</LawCentreID>
<LawCentreName>Secondary Law Centre</LawCentreName>
<Address>
<Address1>ADDRESS11</Address1>
<Address2>ADDRESS21</Address2>
<Address3>ADDRESS31</Address3>
<CountryRef>10985</CountryRef>
<CountyRef>12116</CountyRef>
</Address>
</LawCentre>
</LawCentres>
and i have the following code written , but i am having difficuty extracting the Addresses along with the law centre info:
XDocument lawCentres = XDocument.Load(Server.MapPath(“XML\LawCentres.xml”));
var lawCentreQuery = from c in lawCentres.Descendants("LawCentre")
where (int)c.Element("LawCentreID") == lawCentreID
select new LawCentre
{
LawCentreName = (string)c.Element("LawCentreName"),
LawCentreID = (int)c.Element("LawCentreID"),
LawCentreAddressLine1 = (string)c.Element("Address1"),
LawCentreAddressLine2 = (string)c.Element("Address2"),
LawCentreAddressLine3 = (string)c.Element("Address3"),
};
Any ideas ?
replace
(string)c.Element("Address1")with(string)c.Element("Address").Element("Address1")OR