开发者

How to search XML using LINQ-to_XML Query

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 structu开发者_StackOverflow中文版re="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>


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜