Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 597461
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T16:17:00+00:00 2026-05-13T16:17:00+00:00

I have hierarchical data stored in an XML file. There are multiple companies, each

  • 0

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 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T16:17:01+00:00Added an answer on May 13, 2026 at 4:17 pm

    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
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a bunch of hierarchical data stored in an XML file. I am
I have some hierarchical data - each entry has an id and a (nullable)
I have a table containing hierarchical data. There are currently ~8 levels in this
I have a hierarchical structure stored in a table. Each element has a pointer
I have an issue. I have hierarchical XML data such as: <Tree> <Node Text=Stuff
I have hierarchical data stored in the datastore using a model which looks like
Is there a quick way to get a XML sitemap into a hierarchical data
I have a stored procedure that returns hierarchical data in a single long row,
I have a hierarchical XML file received from client, i need to store it
I have a class which models all categories and they can be ordered hierarchically.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.