C#使用嵌套表读取XML文件

时间:2018-01-11 17:57:52

标签: c# xml

您好我如何使用C#读取带有嵌套表的xml文件,并使用这样的向下钻取数据进行显示。我尝试了DataSet.ReadXml方法,但这些表是单独存储而没有关系。它们可以通过ds.Tables [1],ds.Tables [2]访问。我希望能够显示关系,所以我可以显示如下数据。

ParentTable_Column1,ChildTable1_Column1,ChildTable2_Column1

这是架构

    <?xml version="1.0" encoding="UTF-8"?>
<xs:schema targetNamespace="http://sbmex.dsh.cms.gov" elementFormDefault="qualified" xmlns="http://sbmex.dsh.cms.gov" xmlns:xs="http://www.w3.org/2001/XMLSchema">
    <xs:complexType name="T5_MemberDates">
        <xs:sequence>
            <xs:element name="MemberStartDate" maxOccurs="unbounded" type="xs:date"/>
            <xs:element name="MemberEndDate" maxOccurs="unbounded" type="xs:date"/>
        </xs:sequence>
    </xs:complexType>
    <xs:element name="EPSExtract">
        <xs:complexType>
            <xs:sequence>
                <xs:element name="FileInformation" maxOccurs="unbounded">
                    <xs:complexType>
                        <xs:sequence>
                            <xs:element name="FileId" maxOccurs="unbounded" type="xs:string"/>
                            <xs:element name="FileCreateDateTime" maxOccurs="unbounded" type="xs:dateTime"/>
                            <xs:element name="StateCd" maxOccurs="unbounded" type="xs:string"/>
                            <xs:element name="CoverageYear" maxOccurs="unbounded" type="xs:short"/>
                            <xs:element name="PayeeId" maxOccurs="unbounded" type="xs:string"/>
                            <xs:element name="StateTPID" maxOccurs="unbounded" type="xs:string"/>
                            <xs:element name="IssuerId" maxOccurs="unbounded" type="xs:int"/>
                        </xs:sequence>
                    </xs:complexType>
                </xs:element>
                <xs:element name="Policy" maxOccurs="unbounded">
                    <xs:complexType>
                        <xs:sequence>
                            <xs:element name="OriginalSBMIFileId" maxOccurs="unbounded" type="xs:long"/>
                            <xs:element name="OriginalSBMIExtractDateTime" maxOccurs="unbounded" type="xs:dateTime"/>
                            <xs:element name="CMSPolicyVersionDateTime" maxOccurs="unbounded" type="xs:dateTime"/>
                            <xs:element name="QHPId" maxOccurs="unbounded" type="xs:string"/>
                            <xs:element name="ExchangeAssignedPolicyId" maxOccurs="unbounded" type="xs:int"/>
                            <xs:element name="ExchangeAssignedSubscriberId" maxOccurs="unbounded" type="xs:int"/>
                            <xs:element name="PolicyStartDate" maxOccurs="unbounded" type="xs:date"/>
                            <xs:element name="PolicyEndDate" maxOccurs="unbounded" type="xs:date"/>
                            <xs:element name="EffectuationInd" maxOccurs="unbounded" type="xs:string"/>
                            <xs:element name="InsuranceLineCode" maxOccurs="unbounded" type="xs:string"/>
                            <xs:element name="MemberInformation" maxOccurs="unbounded">
                                <xs:complexType>
                                    <xs:sequence>
                                        <xs:element name="ExchangeAssignedMemberId" maxOccurs="unbounded" type="xs:int"/>
                                        <xs:element name="SubscriberIndicator" maxOccurs="unbounded" type="xs:string"/>
                                        <xs:element name="MemberLastName" maxOccurs="unbounded" type="xs:string"/>
                                        <xs:element name="MemberFirstName" maxOccurs="unbounded" type="xs:string"/>
                                        <xs:choice>
                                            <xs:sequence>
                                                <xs:element name="NameSuffix" maxOccurs="unbounded" type="xs:string"/>
                                                <xs:element name="BirthDate" maxOccurs="unbounded" type="xs:date"/>
                                                <xs:element name="SocialSecurityNumber" maxOccurs="unbounded" type="xs:int"/>
                                                <xs:element name="PostalCode" maxOccurs="unbounded" type="xs:int"/>
                                                <xs:sequence minOccurs="0">
                                                    <xs:element name="LanguageQualifierCode" maxOccurs="unbounded" type="xs:string"/>
                                                    <xs:element name="LanguageCode" maxOccurs="unbounded" type="xs:string"/>
                                                </xs:sequence>
                                                <xs:element name="GenderCode" maxOccurs="unbounded" type="xs:string"/>
                                                <xs:element name="TobaccoUseCode" maxOccurs="unbounded" type="xs:string"/>
                                                <xs:element name="MemberDates" maxOccurs="unbounded" type="T5_MemberDates"/>
                                            </xs:sequence>
                                            <xs:sequence>
                                                <xs:element name="MemberMiddleName" maxOccurs="unbounded" type="xs:string"/>
                                                <xs:choice>
                                                    <xs:sequence>
                                                        <xs:element name="NameSuffix" maxOccurs="unbounded" type="xs:string"/>
                                                        <xs:element name="BirthDate" maxOccurs="unbounded" type="xs:date"/>
                                                        <xs:element name="SocialSecurityNumber" maxOccurs="unbounded" type="xs:int"/>
                                                        <xs:element name="PostalCode" maxOccurs="unbounded" type="xs:int"/>
                                                        <xs:sequence minOccurs="0">
                                                            <xs:element name="LanguageQualifierCode" maxOccurs="unbounded" type="xs:string"/>
                                                            <xs:element name="LanguageCode" maxOccurs="unbounded" type="xs:string"/>
                                                        </xs:sequence>
                                                        <xs:element name="GenderCode" maxOccurs="unbounded" type="xs:string"/>
                                                        <xs:element name="TobaccoUseCode" maxOccurs="unbounded" type="xs:string"/>
                                                        <xs:element name="MemberDates" maxOccurs="unbounded" type="T5_MemberDates"/>
                                                    </xs:sequence>
                                                    <xs:sequence>
                                                        <xs:element name="BirthDate" maxOccurs="unbounded" type="xs:date"/>
                                                        <xs:choice>
                                                            <xs:sequence>
                                                                <xs:element name="SocialSecurityNumber" maxOccurs="unbounded" type="xs:int"/>
                                                                <xs:element name="PostalCode" maxOccurs="unbounded" type="xs:int"/>
                                                                <xs:sequence minOccurs="0">
                                                                    <xs:element name="LanguageQualifierCode" maxOccurs="unbounded" type="xs:string"/>
                                                                    <xs:element name="LanguageCode" maxOccurs="unbounded" type="xs:string"/>
                                                                </xs:sequence>
                                                                <xs:element name="GenderCode" maxOccurs="unbounded" type="xs:string"/>
                                                                <xs:element name="TobaccoUseCode" maxOccurs="unbounded" type="xs:string"/>
                                                                <xs:element name="MemberDates" maxOccurs="unbounded" type="T5_MemberDates"/>
                                                            </xs:sequence>
                                                            <xs:sequence>
                                                                <xs:element name="PostalCode" maxOccurs="unbounded" type="xs:int"/>
                                                                <xs:element name="GenderCode" maxOccurs="unbounded" type="xs:string"/>
                                                                <xs:element name="TobaccoUseCode" maxOccurs="unbounded" type="xs:string"/>
                                                                <xs:element name="MemberDates" maxOccurs="unbounded" type="T5_MemberDates"/>
                                                            </xs:sequence>
                                                        </xs:choice>
                                                    </xs:sequence>
                                                </xs:choice>
                                            </xs:sequence>
                                            <xs:sequence>
                                                <xs:element name="BirthDate" maxOccurs="unbounded" type="xs:date"/>
                                                <xs:element name="SocialSecurityNumber" minOccurs="0" maxOccurs="unbounded" type="xs:int"/>
                                                <xs:element name="PostalCode" maxOccurs="unbounded" type="xs:int"/>
                                                <xs:sequence minOccurs="0">
                                                    <xs:element name="LanguageQualifierCode" maxOccurs="unbounded" type="xs:string"/>
                                                    <xs:element name="LanguageCode" maxOccurs="unbounded" type="xs:string"/>
                                                </xs:sequence>
                                                <xs:element name="GenderCode" maxOccurs="unbounded" type="xs:string"/>
                                                <xs:element name="TobaccoUseCode" maxOccurs="unbounded" type="xs:string"/>
                                                <xs:element name="MemberDates" maxOccurs="unbounded" type="T5_MemberDates"/>
                                            </xs:sequence>
                                        </xs:choice>
                                    </xs:sequence>
                                </xs:complexType>
                            </xs:element>
                            <xs:element name="FinancialInformation" maxOccurs="unbounded">
                                <xs:complexType>
                                    <xs:sequence>
                                        <xs:element name="FinancialEffectiveStartDate" maxOccurs="unbounded" type="xs:date"/>
                                        <xs:element name="FinancialEffectiveEndDate" maxOccurs="unbounded" type="xs:date"/>
                                        <xs:element name="MonthlyTotalPremiumAmount" maxOccurs="unbounded" type="xs:decimal"/>
                                        <xs:element name="MonthlyTotalIndividualResponsibilityAmount" maxOccurs="unbounded" type="xs:decimal"/>
                                        <xs:choice>
                                            <xs:sequence>
                                                <xs:element name="MonthlyCSRAmount" minOccurs="0" maxOccurs="unbounded" type="xs:decimal"/>
                                                <xs:element name="CSRVariantId" maxOccurs="unbounded" type="xs:byte"/>
                                                <xs:element name="RatingArea" maxOccurs="unbounded" type="xs:string"/>
                                            </xs:sequence>
                                            <xs:sequence>
                                                <xs:element name="MonthlyAPTCAmount" maxOccurs="unbounded" type="xs:decimal"/>
                                                <xs:element name="MonthlyCSRAmount" minOccurs="0" maxOccurs="unbounded" type="xs:decimal"/>
                                                <xs:element name="CSRVariantId" maxOccurs="unbounded" type="xs:byte"/>
                                                <xs:element name="RatingArea" maxOccurs="unbounded" type="xs:string"/>
                                            </xs:sequence>
                                        </xs:choice>
                                    </xs:sequence>
                                </xs:complexType>
                            </xs:element>
                        </xs:sequence>
                    </xs:complexType>
                </xs:element>
                <xs:element name="RecentErrorReporting" maxOccurs="unbounded">
                    <xs:complexType>
                        <xs:sequence>
                            <xs:element name="ActivityInd" maxOccurs="unbounded" type="xs:string"/>
                            <xs:element name="PolicyError" maxOccurs="unbounded">
                                <xs:complexType>
                                    <xs:sequence>
                                        <xs:element name="SourceFileId" maxOccurs="unbounded" type="xs:long"/>
                                        <xs:element name="SourceFileCreateDateTime" maxOccurs="unbounded" type="xs:dateTime"/>
                                        <xs:element name="RecordControlNumber" maxOccurs="unbounded" type="xs:int"/>
                                        <xs:element name="ExchangeAssignedPolicyId" maxOccurs="unbounded" type="xs:int"/>
                                        <xs:element name="QHPId" maxOccurs="unbounded" type="xs:string"/>
                                        <xs:element name="ExchangeAssignedSubscriberId" maxOccurs="unbounded" type="xs:int"/>
                                        <xs:element name="Error" maxOccurs="unbounded">
                                            <xs:complexType>
                                                <xs:sequence>
                                                    <xs:element name="ErrorCode" maxOccurs="unbounded" type="xs:string"/>
                                                    <xs:element name="ErrorDescription" maxOccurs="unbounded" type="xs:string"/>
                                                    <xs:element name="ElementInError" maxOccurs="unbounded" type="xs:string"/>
                                                    <xs:element name="AdditionalErrorInfo" minOccurs="0" maxOccurs="unbounded" type="xs:string"/>
                                                </xs:sequence>
                                            </xs:complexType>
                                        </xs:element>
                                    </xs:sequence>
                                </xs:complexType>
                            </xs:element>
                        </xs:sequence>
                    </xs:complexType>
                </xs:element>
            </xs:sequence>
        </xs:complexType>
    </xs:element>
</xs:schema>



<EPSExtract xmlns="http://sbmex.dsh.cms.gov">
    <FileInformation>
        <FileId>Abc123</FileId>
        <FileCreateDateTime>2017-12-21T09:10:31.798000</FileCreateDateTime>
        <StateCd>CA</StateCd>
        <CoverageYear>2018</CoverageYear>
        <PayeeId>A102001</PayeeId>
        <StateTPID>FEP0099CA</StateTPID>
        <IssuerId>123455</IssuerId>
    </FileInformation>
    <Policy xmlns="http://sbmex.dsh.cms.gov">
        <OriginalSBMIFileId>987654</OriginalSBMIFileId>
        <OriginalSBMIExtractDateTime>2017-12-01T01:33:49</OriginalSBMIExtractDateTime>
        <CMSPolicyVersionDateTime>2017-12-07T15:14:49</CMSPolicyVersionDateTime>
        <QHPId>123456</QHPId>
        <ExchangeAssignedPolicyId>55555</ExchangeAssignedPolicyId>
        <ExchangeAssignedSubscriberId>17858469</ExchangeAssignedSubscriberId>
        <PolicyStartDate>2018-01-01</PolicyStartDate>
        <PolicyEndDate>2018-12-31</PolicyEndDate>
        <EffectuationInd>Y</EffectuationInd>
        <InsuranceLineCode>HLT</InsuranceLineCode>
        <MemberInformation>
            <ExchangeAssignedMemberId>17858479</ExchangeAssignedMemberId>
            <SubscriberIndicator>N</SubscriberIndicator>
            <MemberLastName>Thomson</MemberLastName>
            <MemberFirstName>James</MemberFirstName>
            <BirthDate>1988-11-18</BirthDate>
            <SocialSecurityNumber>885954478</SocialSecurityNumber>
            <PostalCode>94112</PostalCode>
            <LanguageQualifierCode>LE</LanguageQualifierCode>
            <LanguageCode>SPA</LanguageCode>
            <GenderCode>M</GenderCode>
            <TobaccoUseCode>U</TobaccoUseCode>
            <MemberDates>
                <MemberStartDate>2018-01-01</MemberStartDate>
                <MemberEndDate>2018-12-31</MemberEndDate>
            </MemberDates>
        </MemberInformation>
        <MemberInformation>
            <ExchangeAssignedMemberId>695414</ExchangeAssignedMemberId>
            <SubscriberIndicator>Y</SubscriberIndicator>
            <MemberLastName>Martin</MemberLastName>
            <MemberFirstName>Yan</MemberFirstName>
            <BirthDate>1969-07-07</BirthDate>
            <SocialSecurityNumber>845591254</SocialSecurityNumber>
            <PostalCode>94112</PostalCode>
            <LanguageQualifierCode>LE</LanguageQualifierCode>
            <LanguageCode>ENG</LanguageCode>
            <GenderCode>M</GenderCode>
            <TobaccoUseCode>U</TobaccoUseCode>
            <MemberDates>
                <MemberStartDate>2018-01-01</MemberStartDate>
                <MemberEndDate>2018-12-31</MemberEndDate>
            </MemberDates>
        </MemberInformation>
        <FinancialInformation>
            <FinancialEffectiveStartDate>2018-01-01</FinancialEffectiveStartDate>
            <FinancialEffectiveEndDate>2018-12-31</FinancialEffectiveEndDate>
            <MonthlyTotalPremiumAmount>929</MonthlyTotalPremiumAmount>
            <MonthlyTotalIndividualResponsibilityAmount>55.08</MonthlyTotalIndividualResponsibilityAmount>
            <MonthlyAPTCAmount>873.92</MonthlyAPTCAmount>
            <MonthlyCSRAmount>204.38</MonthlyCSRAmount>
            <CSRVariantId>05</CSRVariantId>
            <RatingArea>R-667</RatingArea>
        </FinancialInformation>
    </Policy>
</EPSExtract>

1 个答案:

答案 0 :(得分:1)

见下面的代码。我将所有数据放入一个表中,但您可以根据需要创建多个表并添加键,这样您就可以在表中使用公共字段。将所有数据放入一列没有任何问题,除了它将在数据库中使用更多空间。将公共数据拆分成表可以节省数据库中的空间。我将两列数据放入表中,因此您必须完成代码:

root.destroy