Linq to XML 后代元素

发布于 2024-12-05 02:29:12 字数 1774 浏览 0 评论 0原文

我知道这是一个简单的问题,所以很抱歉,但我是 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

七分※倦醒 2024-12-12 02:29:12

(string)c.Element("Address1") 替换为 (string)c.Element("Address").Element("Address1")

(string)c.Descendants("Address1").FirstOrDefault()

replace (string)c.Element("Address1") with (string)c.Element("Address").Element("Address1")

OR

(string)c.Descendants("Address1").FirstOrDefault()
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文