如何使用 LINQ-to_XML 查询搜索 XML

发布于 2024-08-19 23:04:23 字数 3436 浏览 4 评论 0原文

我将分层数据存储在 XML 文件中。有多个公司,每个公司可以有多个业务线。他们在多个州开展每项业务。每个州都可以有多种费率结构。下面显示了一个伪造的示例。

例如,如何编写 LINQ to XML 查询来返回给定公司、业务线和州的所有费率结构?或者某家公司提供人寿保险的所有州。

示例:返回俄勒冈州 State Farm 地震保险的所有费率。

示例:返回 Travelers 提供人寿保险的所有州。

我知道如何深入一层,但不知道如何进一步深入。我只知道一旦得到答案我会拍拍我的头并“呃”,但现在我被困住了。

<?xml version="1.0" encoding="utf-8" ?>
<businessData>
  <company name="StateFarm" id="21">
    <lineOfBusiness name="Homeowners" id="24">
      <state name="Texas" abbreviation="TX">
        <rate structure="A"/>
        <rate structure="D"/>
        <rate structure="F"/>
      </state>
    </lineOfBusiness>
    <lineOfBusiness name="Earthquake" id="62">
      <state name="California" abbreviation="CA">
        <rate structure="A"/>
        <rate structure="B"/>
      </state>
      <state name="Oregon" abbreviation="OR">
        <rate structure="A"/>
      </state>
      <state name="Washington" abbreviation="WA">
        <rate structure="A"/>
      </state>
    </lineOfBusiness>
    <lineOfBusiness name="Fire" id="22">
      <state name="California" abbreviation="CA">
        <rate structure="A"/>
      </state>
    </lineOfBusiness>
  </company>
  <company name="Travellers" id="17">
    <lineOfBusiness name="Life" id="23">
      <state name="Florida" abbreviation="FL">
        <rate structure="A"/>
        <rate structure="C"/>
        <rate structure="D"/>
      </state>
      <state name="Alabama" abbreviation="AL">
        <rate structure="A"/>
        <rate structure="B"/>
        <rate structure="C"/>
      </state>
    </lineOfBusiness>
    <lineOfBusiness name="Homeowners" id="24">
      <state name="Alabama" abbreviation="AL">
        <rate structure="X"/>
        <rate structure="Y"/>
        <rate structure="X"/>
      </state>
      <state name="Arkansas" abbreviation="AR">
        <rate structure="C"/>
      </state>
      <state name="California" abbreviation="CA">
        <rate structure="G"/>
      </state>
      <state name="Florida" abbreviation="FL">
        <rate structure="D"/>
      </state>
      <state name="Georgia" abbreviation="GA">
        <rate structure="D"/>
      </state>
      <state name="Louisiana" abbreviation="LA">
        <rate structure="B"/>
      </state>
      <state name="Missouri" abbreviation="MO">
        <rate structure="A"/>
      </state>
    </lineOfBusiness>
    <lineOfBusiness name="Auto" id="25">
      <state name="California" abbreviation="CA">
        <rate structure="T"/>
        <rate structure="Y"/>
        <rate structure="Z"/>
      </state>
    </lineOfBusiness>
  </company>
  <company name="NationWide" id="79">
    <lineOfBusiness name="Earthquake" code="EQ" id="62">
      <state name="California" abbreviation="CA">
        <rate structure="B"/>
        <rate structure="C"/>
        <rate structure="D"/>
        <rate structure="G"/>
      </state>
    </lineOfBusiness>
  </company>
</businessData>

I have hierarchical data stored in an XML file. There are multiple companies, each can have multiple lines of business. They conduct each line of business in multiple states. And in each state there can be multiple rates structures. A bogus sample is shown below.

How do I write a LINQ to XML query to return, for example, all the rates structures for a given company, line of business, and state? Or all the states in which a given company offers life insurance.

Example: return all the rates for State Farm Earthquake insurance in Oregon.

Example: return all the states in which Travellers offers Life insurance.

I know how to do this one level deep but can't figure out how to drill down deeper than that. I just know I'll slap my head and go "Duh" once I get an answer, but for now I'm stuck.

<?xml version="1.0" encoding="utf-8" ?>
<businessData>
  <company name="StateFarm" id="21">
    <lineOfBusiness name="Homeowners" id="24">
      <state name="Texas" abbreviation="TX">
        <rate structure="A"/>
        <rate structure="D"/>
        <rate structure="F"/>
      </state>
    </lineOfBusiness>
    <lineOfBusiness name="Earthquake" id="62">
      <state name="California" abbreviation="CA">
        <rate structure="A"/>
        <rate structure="B"/>
      </state>
      <state name="Oregon" abbreviation="OR">
        <rate structure="A"/>
      </state>
      <state name="Washington" abbreviation="WA">
        <rate structure="A"/>
      </state>
    </lineOfBusiness>
    <lineOfBusiness name="Fire" id="22">
      <state name="California" abbreviation="CA">
        <rate structure="A"/>
      </state>
    </lineOfBusiness>
  </company>
  <company name="Travellers" id="17">
    <lineOfBusiness name="Life" id="23">
      <state name="Florida" abbreviation="FL">
        <rate structure="A"/>
        <rate structure="C"/>
        <rate structure="D"/>
      </state>
      <state name="Alabama" abbreviation="AL">
        <rate structure="A"/>
        <rate structure="B"/>
        <rate structure="C"/>
      </state>
    </lineOfBusiness>
    <lineOfBusiness name="Homeowners" id="24">
      <state name="Alabama" abbreviation="AL">
        <rate structure="X"/>
        <rate structure="Y"/>
        <rate structure="X"/>
      </state>
      <state name="Arkansas" abbreviation="AR">
        <rate structure="C"/>
      </state>
      <state name="California" abbreviation="CA">
        <rate structure="G"/>
      </state>
      <state name="Florida" abbreviation="FL">
        <rate structure="D"/>
      </state>
      <state name="Georgia" abbreviation="GA">
        <rate structure="D"/>
      </state>
      <state name="Louisiana" abbreviation="LA">
        <rate structure="B"/>
      </state>
      <state name="Missouri" abbreviation="MO">
        <rate structure="A"/>
      </state>
    </lineOfBusiness>
    <lineOfBusiness name="Auto" id="25">
      <state name="California" abbreviation="CA">
        <rate structure="T"/>
        <rate structure="Y"/>
        <rate structure="Z"/>
      </state>
    </lineOfBusiness>
  </company>
  <company name="NationWide" id="79">
    <lineOfBusiness name="Earthquake" code="EQ" id="62">
      <state name="California" abbreviation="CA">
        <rate structure="B"/>
        <rate structure="C"/>
        <rate structure="D"/>
        <rate structure="G"/>
      </state>
    </lineOfBusiness>
  </company>
</businessData>

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

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

发布评论

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

评论(1

终陌 2024-08-26 23:04:23

示例:返回俄勒冈州 State Farm 地震保险的所有费率:

var result = from company in XDocument.Load("test.xml").Root.Elements("company")
             from lineOfBusiness in company.Elements("lineOfBusiness")
             from state in lineOfBusiness.Elements("state")
             where company.Attributes("name").First().Value == "StateFarm" && 
                   lineOfBusiness.Attributes("name").First().Value == "Earthquake" &&
                   state.Attributes("name").First().Value == "Oregon"
             select state.Elements("rate");

示例:返回 Travelers 提供人寿保险的所有州:

var result = from company in XDocument.Load("test.xml").Root.Elements("company")
             from lineOfBusiness in company.Elements("lineOfBusiness")
             from state in lineOfBusiness.Elements("state")
             where company.Attributes("name").First().Value == "Travellers" &&
                   lineOfBusiness.Attributes("name").First().Value == "Life"
             select state.Attributes("name").First().Value;

当然,这假设 XML 文档针对 XSD 架构作为名称的有效性 > 属性应该存在。

Example: return all the rates for State Farm Earthquake insurance in Oregon:

var result = from company in XDocument.Load("test.xml").Root.Elements("company")
             from lineOfBusiness in company.Elements("lineOfBusiness")
             from state in lineOfBusiness.Elements("state")
             where company.Attributes("name").First().Value == "StateFarm" && 
                   lineOfBusiness.Attributes("name").First().Value == "Earthquake" &&
                   state.Attributes("name").First().Value == "Oregon"
             select state.Elements("rate");

Example: return all the states in which Travellers offers Life insurance:

var result = from company in XDocument.Load("test.xml").Root.Elements("company")
             from lineOfBusiness in company.Elements("lineOfBusiness")
             from state in lineOfBusiness.Elements("state")
             where company.Attributes("name").First().Value == "Travellers" &&
                   lineOfBusiness.Attributes("name").First().Value == "Life"
             select state.Attributes("name").First().Value;

Of course this assumes the validity of the XML document against an XSD schema as the name attribute should be present.

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