C# LINQ XML Query with duplicate element names that have attributes

Posted by ncain187 on Stack Overflow See other posts from Stack Overflow or by ncain187
Published on 2010-03-09T04:34:15Z Indexed on 2010/03/09 4:36 UTC
Read the original article Hit count: 361

Filed under:
|
|
|
        <Party id="Party_1">
            <PartyTypeCode tc="1">Person</PartyTypeCode>
            <FullName>John Doe</FullName>
            <GovtID>123456789</GovtID>
            <GovtIDTC tc="1">Social Security Number US</GovtIDTC>
            <ResidenceState tc="35">New Jersey</ResidenceState>
            <Person>
                <FirstName>Frank</FirstName>
                <MiddleName>Roberts</MiddleName>
                <LastName>Madison</LastName>                    
                <Prefix>Dr.</Prefix>
                <Suffix>III</Suffix>
                <Gender tc="1">Male</Gender>
                <BirthDate>1974-01-01</BirthDate>
                <Age>35</Age>
                <Citizenship tc="1">United States of America</Citizenship>
            </Person>
            <Address>
                <AddressTypeCode tc="26">Bill Mailing</AddressTypeCode>
                <Line1>2400 Meadow Lane</Line1>
                <Line2></Line2>
                <Line3></Line3>
                <Line4></Line4>
                <City>Somerset</City>
                <AddressStateTC tc="35">New Jersey</AddressStateTC>
                <Zip>07457</Zip>
                <AddressCountryTC tc="1">United States of America</AddressCountryTC>
            </Address>
        </Party>
        <!-- ***********************  -->
        <!--  Insured Information     -->
        <!-- ***********************  -->
        <Party id="Party_2">
            <PartyTypeCode tc="1">Person</PartyTypeCode>
            <FullName>Dollie Robert Madison</FullName>
            <GovtID>123956239</GovtID>
            <GovtIDTC tc="1">Social Security Number US</GovtIDTC>
            <Person>
                <FirstName>Dollie</FirstName>
                <MiddleName>R</MiddleName>
                <LastName>Madison</LastName>
                <Suffix>III</Suffix>
                <Gender tc="2">Female</Gender>
                <BirthDate>1996-10-12</BirthDate>
                <Citizenship tc="1">United States of America</Citizenship>
            </Person>
            <!-- Insured Address -->
            <Address>
                <AddressTypeCode tc="26">Bill Mailing</AddressTypeCode>
                <Line1>2400 Meadow Lane</Line1>
                <City>Somerset</City>
                <AddressStateTC tc="35">New Jersey</AddressStateTC>
                <Zip>07457</Zip>
                <AddressCountryTC tc="1">United States of America</AddressCountryTC>
            </Address>
            <Risk>
                <!-- Disability Begin Effective Date -->
                <DisabilityEffectiveStartDate>2006-01-01</DisabilityEffectiveStartDate>
                <!-- Disability End Effective Date -->
                <DisabilityEffectiveStopDate>2008-01-01</DisabilityEffectiveStopDate>
            </Risk>
        </Party>
        <!-- *******************************  -->
        <!--  Company Information     -->
        <!-- ******************************  -->
        <Party id="Party_3">
            <PartyTypeCode tc="2">Organization</PartyTypeCode>
            <Organization>
                <DTCCMemberCode>1234</DTCCMemberCode>
            </Organization>
            <Carrier>
                <CarrierCode>105</CarrierCode>
            </Carrier>
        </Party>

Here is my code which doesn't work because party 3 doesn't contain FullName, I know that partyelements contains 3 parties if I only return the name attribute. Is there a way to loop through each tag seperate?

        var partyElements = from party in xmlDoc.Descendants("Party")
           select new
           {
               Name = party.Attribute("id").Value,
               PartyTypeCode = party.Element("PartyTypeCode").Value,
               FullName = party.Element("FullName").Value,
               GovtID = party.Element("GovtID").Value,
           };

© Stack Overflow or respective owner

Related posts about c#

Related posts about LINQ