我知道这是一个简单的问题,所以道歉,但我是linq to xml的新手,并且遇到以下困难:
我有以下XML,并试图找出所有元素,包括地址元素:
<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>
并且我编写了以下代码,但我正在使用difficuty提取地址以及法律中心信息: 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"),
};
有什么想法吗?
答案 0 :(得分:0)
将(string)c.Element("Address1")
替换为(string)c.Element("Address").Element("Address1")
OR
(string)c.Descendants("Address1").FirstOrDefault()