需要 LINQ to XML 深度解析帮助

发布于 2024-12-28 12:51:19 字数 3038 浏览 1 评论 0原文

我是 LINQ to XML 的新手,我一直在查看教程,但没有找到足够深入的教程来帮助我克服障碍。就我而言,我联系了我们的供应商以获取有关给定地址的信息。该地址可能不完全匹配,因此他们向我发回一个包含可能地址的 XML 文件。

下面是从地址查询返回的多个地址列表的给定 XML 块:

    <POSSIBLE-ADDRESSES>
              <POS-ADDR>
                <BUILDING>
                  <Street>1905 W HENDERSON RD                                      </Street>
                  <City>COLUMBUS                      </City>
                  <RiskID>123456</RiskID>
                </BUILDING>
                <OCCUPANTS>
                  <OCCUP>
                    <ID>010</ID>
                    <Desc>MC DONALD'S RESTAURANT (1S)        </Desc>
                  </OCCUP>
                  <OCCUP>
                    <ID>015</ID>
                    <Desc>MC DONALD'S RESTAURANT             </Desc>
                  </OCCUP>
                </OCCUPANTS>
              </POS-ADDR>
              <POS-ADDR>
                <BUILDING>
                  <Street>1821 HENDERSON RD                                        </Street>
                  <City>UPPER ARLINGTON               </City>
                  <RiskID>1234567</RiskID>
                </BUILDING>
                <OCCUPANTS>
                  <OCCUP>
                    <ID>010</ID>
                    <Desc>ARLINGTON SQUARE SHOPPING CTR (1S) </Desc>
                  </OCCUP>
                  <OCCUP>
                    <ID>015</ID>
                    <Desc>1821 SWAN DRY CLNG                 </Desc>
                  </OCCUP>
                  <OCCUP>
                    <ID>020</ID>
                    <Desc>4681 ALEX'S BISTRO ON REED/REST    </Desc>
                  </OCCUP>
                  <OCCUP>
                    <ID>025</ID>
                    <Desc>4687-93 BLUMEN GARTEN              </Desc>
                  </OCCUP>
                  <OCCUP>
                    <ID>030</ID>
                    <Desc>4697 BLIMPIE/SANDWICH SHOP         </Desc>
                  </OCCUP>
                </OCCUPANTS>
              </POS-ADDR>
            </POSSIBLE-ADDRESSES>

现在我要做的就是使用以下命令拉出所有建筑物(请注意,可能还有更多),然后将其绑定到一个列表盒子 - 没什么大不了的:

    var qBuildings = from LOP in loaded.Descendants("BUILDING")
    select new
    {
      BuildingName = LOP.Element("Street").Value,
      RiskId = LOP.Element("RiskID").Value
    };

这为我提供了我需要的所有建筑物及其风险 ID。

现在的问题是,当用户根据风险 ID(例如第一个 RiskID“123456”)选择给定建筑物时,如何拉出“居住者”。因此,如果用户选择该建筑物,它将出去并抓住每个居住者,为居住者类别加水,并将该类别添加到居住者列表中,然后我将其绑定到另一个列表框。

到目前为止,我所做的一切尝试都只是试图吸引住户,但结果都不好。我知道这一定是导航问题,但我似乎无法以返回任何有用内容的方式构建查询。每个风险 ID 都是唯一的,并且每个建筑物可以没有或有多个居住者。所以我不知道如何拉住乘客。

任何帮助将不胜感激。

I'm new to LINQ to XML and I have been looking at tutorials but I've not found one that has sufficient depth to get me past my block. In my case I hit a vendor of ours for information on a given address. There is the possibility that the address will not be an exact match so they send me back an XML file that has possible addresses.

Here is a given chunk of XML for a multiple list of addresses coming back from an address query:

    <POSSIBLE-ADDRESSES>
              <POS-ADDR>
                <BUILDING>
                  <Street>1905 W HENDERSON RD                                      </Street>
                  <City>COLUMBUS                      </City>
                  <RiskID>123456</RiskID>
                </BUILDING>
                <OCCUPANTS>
                  <OCCUP>
                    <ID>010</ID>
                    <Desc>MC DONALD'S RESTAURANT (1S)        </Desc>
                  </OCCUP>
                  <OCCUP>
                    <ID>015</ID>
                    <Desc>MC DONALD'S RESTAURANT             </Desc>
                  </OCCUP>
                </OCCUPANTS>
              </POS-ADDR>
              <POS-ADDR>
                <BUILDING>
                  <Street>1821 HENDERSON RD                                        </Street>
                  <City>UPPER ARLINGTON               </City>
                  <RiskID>1234567</RiskID>
                </BUILDING>
                <OCCUPANTS>
                  <OCCUP>
                    <ID>010</ID>
                    <Desc>ARLINGTON SQUARE SHOPPING CTR (1S) </Desc>
                  </OCCUP>
                  <OCCUP>
                    <ID>015</ID>
                    <Desc>1821 SWAN DRY CLNG                 </Desc>
                  </OCCUP>
                  <OCCUP>
                    <ID>020</ID>
                    <Desc>4681 ALEX'S BISTRO ON REED/REST    </Desc>
                  </OCCUP>
                  <OCCUP>
                    <ID>025</ID>
                    <Desc>4687-93 BLUMEN GARTEN              </Desc>
                  </OCCUP>
                  <OCCUP>
                    <ID>030</ID>
                    <Desc>4697 BLIMPIE/SANDWICH SHOP         </Desc>
                  </OCCUP>
                </OCCUPANTS>
              </POS-ADDR>
            </POSSIBLE-ADDRESSES>

Now what I do is pull all the buildings (mind you there can be many more than this) with the following and then bind that to a list box - no biggie:

    var qBuildings = from LOP in loaded.Descendants("BUILDING")
    select new
    {
      BuildingName = LOP.Element("Street").Value,
      RiskId = LOP.Element("RiskID").Value
    };

This gives me all the buildings that I need along with their risk ID's.

Now the kicker is how to pull the "occupants" when the user selects a given building based on the risk ID say the first RiskID of "123456". So if the user selects this building it will go out and grab each occupant hydrating an occupant class and add that class to a list of occupants which I will then bind to another list box.

Everything I've tried so far has had bad results at just trying to get the occupants. I know it must be a matter of navigation but I can't seem to build the query in a way that returns anything useful. Each risk ID is unique and each building can have none or many occupants. So I'm lost at how to pull the occupants.

Any help would be appreciated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

罪歌 2025-01-04 12:51:19

在我看来,您想从 pos-addr 中选择居住者,其建筑物具有特定的riskId。在 LINQ-to-XML 中,可能如下所示:

var posAddr =
    (from pos in possibleAddresses.Elements("POS-ADDR")
     where (int)pos.Element("BUILDING").Element("RiskID") == riskIdToLookFor
     select pos)
    .Single();

var occupants =
    from o in posAddr.Element("OCCUPANTS").Elements("OCCUP")
    select new
    {
        Description = (string)o.Element("DESC")
    }

It seems to me you want to select occupants from a pos-addr, whose building has a specific riskId. In LINQ-to-XML, that could look like this:

var posAddr =
    (from pos in possibleAddresses.Elements("POS-ADDR")
     where (int)pos.Element("BUILDING").Element("RiskID") == riskIdToLookFor
     select pos)
    .Single();

var occupants =
    from o in posAddr.Element("OCCUPANTS").Elements("OCCUP")
    select new
    {
        Description = (string)o.Element("DESC")
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文