2

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>
1
  • What language are you using? Could you post "one level deep" code that you have so we can help you see where to go next? Commented Feb 4, 2010 at 18:12

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.