如何使用LINQ-to_XML查询搜索XML [英] How to search XML using LINQ-to_XML Query

查看:65
本文介绍了如何使用LINQ-to_XML查询搜索XML的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将分层数据存储在XML文件中.有多个公司,每个公司可以有多个业务部门.他们在多个州开展每项业务.在每个州中,可以有多个费率结构.伪样本如下所示.

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.

如何编写LINQ to XML查询以返回(例如)给定公司,业务范围和州的所有费率结构?或给定公司提供人寿保险的所有州.

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>

推荐答案

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

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;

当然,这是假定XML文档针对XSD模式的有效性,因为应该存在 name 属性.

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

这篇关于如何使用LINQ-to_XML查询搜索XML的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆