Linq to XML 后代元素
我知道这是一个简单的问题,所以很抱歉,但我是 linq to xml 的新手,并且在以下方面遇到困难:
我有以下 XML 并试图获取所有元素,包括 Address 元素:
<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>
和我编写了以下代码,但我很难提取地址以及法律中心信息: 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"),
};
有什么想法吗?
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 ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
将
(string)c.Element("Address1")
替换为(string)c.Element("Address").Element("Address1")
或
replace
(string)c.Element("Address1")
with(string)c.Element("Address").Element("Address1")
OR