2010-02-04 62 views
2

我有存儲在XML文件中的分層數據。有多家公司,每家可以有多個業務線。他們在多個州開展業務。在每個州都可以有多種費率結構。假樣本如下所示。如何使用LINQ-to_XML搜索XML查詢

如何編寫LINQ to XML查詢以返回給定公司,業務線和州的所有費率結構?或者某個公司提供人壽保險的所有州。

例如:返回俄勒岡州農場地震保險的所有費率。

示例:返回旅行者提供人壽保險的所有州。

我知道如何做到這一級深,但無法弄清楚如何深入鑽研。我只知道,一旦我得到答案,我會打我的頭,然後去「杜」,但現在我卡住了。

<?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> 
+0

您使用哪種語言?您可以發佈您的「一層深」代碼,以便我們可以幫助您瞭解下一步該去哪裏? – CoderDennis 2010-02-04 18:12:36

回答

1

例子:返回所有的費率國營農場地震保險在俄勒岡州:

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"); 

例子:返回所有在旅行者提供壽險狀態:

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模式有效,因爲名稱屬性應該存在。