如何使用 LINQ-to_XML 查询搜索 XML
我将分层数据存储在 XML 文件中。有多个公司,每个公司可以有多个业务线。他们在多个州开展每项业务。每个州都可以有多种费率结构。下面显示了一个伪造的示例。
例如,如何编写 LINQ to XML 查询来返回给定公司、业务线和州的所有费率结构?或者某家公司提供人寿保险的所有州。
示例:返回俄勒冈州 State Farm 地震保险的所有费率。
示例:返回 Travelers 提供人寿保险的所有州。
我知道如何深入一层,但不知道如何进一步深入。我只知道一旦得到答案我会拍拍我的头并“呃”,但现在我被困住了。
<?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>
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>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
示例:返回俄勒冈州 State Farm 地震保险的所有费率:
示例:返回 Travelers 提供人寿保险的所有州:
当然,这假设 XML 文档针对 XSD 架构作为名称的有效性 > 属性应该存在。
Example: return all the rates for State Farm Earthquake insurance in Oregon:
Example: return all the states in which Travellers offers Life insurance:
Of course this assumes the validity of the XML document against an XSD schema as the name attribute should be present.