Hello all,

I have an XML string that I need to tear down and process. Sorry for what may be a simple problem, but I'm new to XML processing.

Here is a sample of the XML:

<?xml version="1.0" encoding="UTF-8" ?>
<CCSMessage Type="4" Name="CustomerFetchResponse" Source="CoremaContextServer" Destination="WSA0205" UserID="LoyaltyBatch" IncludeLoyaltySummary="true" LimitLoyaltyTransactions="0" >
    <Result>
        <Status>SUCCESS</Status>
    </Result>
    <Customer CustomerID="36240">
        <CustomerID CCSID="289521" TimeStamp="2010-10-01 12:13:53">36240
        </CustomerID>
        <LoyaltySummary HouseholdID="110000036242" LoyaltyStatus="2" TotalSpend="6419.80" TotalVisits="136">
            <LoyaltyIDs>
                <LoyaltyID ID="410000073094" RedeemFlag="true" DateIssued="2010-10-01">
                </LoyaltyID>
            </LoyaltyIDs>
            <OfferProgress>
                <OfferProgressTotals OfferID="1012" OfferType="P" OfferDescription="No Email Birthday Promotion for Dec" OptIn="Y">
                    <OfferProgressTotal ID="1" Description="Earned">
                        <LifetimeValue>0</LifetimeValue>
                    </OfferProgressTotal>
                    <OfferProgressTotal ID="2" Description="Last Reward">
                        <LifetimeValue>0</LifetimeValue>
                    </OfferProgressTotal>
                    <OfferProgressTotal ID="3" Description="Redeemed">
                        <LifetimeValue>0</LifetimeValue>
                    </OfferProgressTotal>
                    <OfferProgressTotal ID="4" Description="Total 4">
                        <LifetimeValue>0.00</LifetimeValue>
                    </OfferProgressTotal>
                    <EligibleDate>11/24/2014</EligibleDate>
                    <ExpirationDate>12/31/2014</ExpirationDate>
                    <TotalUsage>-2147483648</TotalUsage>
                    <ExpiringAwards>
                        <ExpiringAward OfferID="1112" OfferDescription="null">
                            <PointsEarned>1</PointsEarned>
                            <PointsRedeemed>1</PointsRedeemed>
                            <PointsExpired>0</PointsExpired>
                            <AwardExpiryDate>1900-01-01</AwardExpiryDate>
                        </ExpiringAward>
                    </ExpiringAwards>
                </OfferProgressTotals>
                <OfferProgressTotals OfferID="2012" OfferType="P" OfferDescription="Email Birthday Coupon for Dec" OptIn="N">
                    <OfferProgressTotal ID="1" Description="Earned">
                        <LifetimeValue>0</LifetimeValue>
                    </OfferProgressTotal>
                    <OfferProgressTotal ID="2" Description="Last Reward">
                        <LifetimeValue>0</LifetimeValue>
                    </OfferProgressTotal>
                    <OfferProgressTotal ID="3" Description="Redeemed">
                        <LifetimeValue>0</LifetimeValue>
                    </OfferProgressTotal>
                    <OfferProgressTotal ID="4" Description="Total 4">
                        <LifetimeValue>0.00</LifetimeValue>
                    </OfferProgressTotal>
                    <EligibleDate>12/02/2014</EligibleDate>
                    <ExpirationDate>12/31/2014</ExpirationDate>
                    <TotalUsage>-2147483648</TotalUsage>
                    <ExpiringAwards>
                    </ExpiringAwards>
                </OfferProgressTotals>
                <OfferProgressTotals OfferID="4008" OfferType="P" OfferDescription="Email Anniversary Coupon for Aug" OptIn="N">
                    <OfferProgressTotal ID="1" Description="Earned">
                        <LifetimeValue>0</LifetimeValue>
                    </OfferProgressTotal>
                    <OfferProgressTotal ID="2" Description="Last Reward">
                        <LifetimeValue>0</LifetimeValue>
                    </OfferProgressTotal>
                    <OfferProgressTotal ID="3" Description="Redeemed">
                        <LifetimeValue>0</LifetimeValue>
                    </OfferProgressTotal>
                    <OfferProgressTotal ID="4" Description="Total 4">
                        <LifetimeValue>0.00</LifetimeValue>
                    </OfferProgressTotal>
                    <EligibleDate>08/01/2014</EligibleDate>
                    <ExpirationDate>08/31/2014</ExpirationDate>
                    <TotalUsage>-2147483648</TotalUsage>
                    <ExpiringAwards>
                    </ExpiringAwards>
                </OfferProgressTotals>
                <OfferProgressTotals OfferID="9001" OfferType="P" OfferDescription="Birthday promotion" OptIn="Y">
                    <OfferProgressTotal ID="1" Description="Earned">
                        <LifetimeValue>1</LifetimeValue>
                    </OfferProgressTotal>
                    <OfferProgressTotal ID="2" Description="Last Reward">
                        <LifetimeValue>0</LifetimeValue>
                    </OfferProgressTotal>
                    <OfferProgressTotal ID="3" Description="Redeemed">
                        <LifetimeValue>0</LifetimeValue>
                    </OfferProgressTotal>
                    <OfferProgressTotal ID="4" Description="Total 4">
                        <LifetimeValue>0.00</LifetimeValue>
                    </OfferProgressTotal>
                    <EligibleDate>01/01/2010</EligibleDate>
                    <ExpirationDate>12/31/2100</ExpirationDate>
                    <TotalUsage>-2147483648</TotalUsage>
                    <ExpiringAwards>
                        <ExpiringAward OfferID="9002" OfferDescription="null">
                            <PointsEarned>1</PointsEarned>
                            <PointsRedeemed>0</PointsRedeemed>
                            <PointsExpired>0</PointsExpired>
                            <AwardExpiryDate>1900-01-01</AwardExpiryDate>
                        </ExpiringAward>
                        <ExpiringAward OfferID="9002" OfferDescription="null">
                            <PointsEarned>1</PointsEarned>
                            <PointsRedeemed>0</PointsRedeemed>
                            <PointsExpired>0</PointsExpired>
                            <AwardExpiryDate>2012-01-12</AwardExpiryDate>
                        </ExpiringAward>
                    </ExpiringAwards>
                </OfferProgressTotals>
                <OfferProgressTotals OfferID="9011" OfferType="P" OfferDescription="Pharmacy promotion" OptIn="Y">
                    <OfferProgressTotal ID="1" Description="Earned">
                        <LifetimeValue>157</LifetimeValue>
                    </OfferProgressTotal>
                    <OfferProgressTotal ID="2" Description="Last Reward">
                        <LifetimeValue>0</LifetimeValue>
                    </OfferProgressTotal>
                    <OfferProgressTotal ID="3" Description="Redeemed">
                        <LifetimeValue>150</LifetimeValue>
                    </OfferProgressTotal>
                    <OfferProgressTotal ID="4" Description="Total 4">
                        <LifetimeValue>0.00</LifetimeValue>
                    </OfferProgressTotal>
                    <EligibleDate>01/01/2010</EligibleDate>
                    <ExpirationDate>12/31/2100</ExpirationDate>
                    <TotalUsage>-2147483648</TotalUsage>
                    <ExpiringAwards>
                        <ExpiringAward OfferID="9012" OfferDescription="null">
                            <PointsEarned>15</PointsEarned>
                            <PointsRedeemed>15</PointsRedeemed>
                            <PointsExpired>0</PointsExpired>
                            <AwardExpiryDate>1900-01-01</AwardExpiryDate>
                        </ExpiringAward>
                    </ExpiringAwards>
                </OfferProgressTotals>
                <OfferProgressTotals OfferID="9021" OfferType="P" OfferDescription="2013 - 10 for 300" OptIn="N">
                    <OfferProgressTotal ID="1" Description="Earned">
                        <LifetimeValue>244523</LifetimeValue>
                    </OfferProgressTotal>
                    <OfferProgressTotal ID="2" Description="Last Reward">
                        <LifetimeValue>0</LifetimeValue>
                    </OfferProgressTotal>
                    <OfferProgressTotal ID="3" Description="Redeemed">
                        <LifetimeValue>240000</LifetimeValue>
                    </OfferProgressTotal>
                    <OfferProgressTotal ID="4" Description="Total 4">
                        <LifetimeValue>0.00</LifetimeValue>
                    </OfferProgressTotal>
                    <EligibleDate>01/02/2012</EligibleDate>
                    <ExpirationDate>12/31/2099</ExpirationDate>
                    <TotalUsage>-2147483648</TotalUsage>
                    <ExpiringAwards>
                        <ExpiringAward OfferID="9022" OfferDescription="">
                            <PointsEarned>8</PointsEarned>
                            <PointsRedeemed>7</PointsRedeemed>
                            <PointsExpired>0</PointsExpired>
                            <AwardExpiryDate>1900-01-01</AwardExpiryDate>
                        </ExpiringAward>
                    </ExpiringAwards>
                </OfferProgressTotals>
                <OfferProgressTotals OfferID="9031" OfferType="P" OfferDescription="2014 - 10 for 300" OptIn="N">
                    <OfferProgressTotal ID="1" Description="Earned">
                        <LifetimeValue>33420</LifetimeValue>
                    </OfferProgressTotal>
                    <OfferProgressTotal ID="2" Description="Last Reward">
                        <LifetimeValue>0</LifetimeValue>
                    </OfferProgressTotal>
                    <OfferProgressTotal ID="3" Description="Redeemed">
                        <LifetimeValue>30000</LifetimeValue>
                    </OfferProgressTotal>
                    <OfferProgressTotal ID="4" Description="Total 4">
                        <LifetimeValue>0.00</LifetimeValue>
                    </OfferProgressTotal>
                    <EligibleDate>01/02/2012</EligibleDate>
                    <ExpirationDate>12/31/2099</ExpirationDate>
                    <TotalUsage>-2147483648</TotalUsage>
                    <ExpiringAwards>
                        <ExpiringAward OfferID="9032" OfferDescription="null">
                            <PointsEarned>0</PointsEarned>
                            <PointsRedeemed>0</PointsRedeemed>
                            <PointsExpired>0</PointsExpired>
                            <AwardExpiryDate>1900-01-01</AwardExpiryDate>
                        </ExpiringAward>
                        <ExpiringAward OfferID="9032" OfferDescription="null">
                            <PointsEarned>1</PointsEarned>
                            <PointsRedeemed>0</PointsRedeemed>
                            <PointsExpired>0</PointsExpired>
                            <AwardExpiryDate>2014-05-29</AwardExpiryDate>
                        </ExpiringAward>
                    </ExpiringAwards>
                </OfferProgressTotals>
                <OfferProgressTotals OfferID="15" OfferType="T" OfferDescription="Track email reminders" OptIn="N">
                    <OfferProgressTotal ID="1" Description="Purchased">
                        <LifetimeValue>2</LifetimeValue>
                    </OfferProgressTotal>
                    <OfferProgressTotal ID="2" Description="Awarded">
                        <LifetimeValue>2</LifetimeValue>
                    </OfferProgressTotal>
                    <OfferProgressTotal ID="3" Description="Total 3">
                        <LifetimeValue>0.00</LifetimeValue>
                    </OfferProgressTotal>
                    <OfferProgressTotal ID="4" Description="Total 4">
                        <LifetimeValue>0.00</LifetimeValue>
                    </OfferProgressTotal>
                    <EligibleDate></EligibleDate>
                    <ExpirationDate></ExpirationDate>
                    <TotalUsage>-2147483648</TotalUsage>
                </OfferProgressTotals>
            </OfferProgress>
        </LoyaltySummary>
    </Customer>
</CCSMessage>

I can get to the <OfferProgressTotals> level just fine with this code:

xDocument.LoadXml(XMLResponse)
doc = XElement.Parse(XMLResponse)

For Each offer As XElement In doc.Descendants("OfferProgressTotals")
    If offer.HasAttributes Then
        TempOfferID = offer.Attribute("OfferID").Value
    End If

    If TempOfferID = _OneAct._Program Then
        If offer.HasElements Then
            Console.WriteLine("Offer ID : " + offer.Attribute("OfferID").Value)
            Console.WriteLine("Eligible Date : " + offer.Element("EligibleDate").Value)
        End If
    End If
Next

As you can see in the XML, there are repeated <OfferProgressTotal> nodes, but I only want to use the ones that are under the <OfferProgressTotals> where the OfferID matches what I am looking for. I have been searching for examples for days, and have not come up with anything.

Thanks!

Here's one way using LINQ:

Dim searchstr = "9001"
doc.Descendants("OfferProgressTotals") _
    .Where(Function(x) x.HasAttributes) _
    .Where(Function(x) x.Attribute("OfferID").Value = searchstr) _
    .First(Function(x)
               Console.WriteLine("Offer ID : {0}" & vbNewLine _
                                 & "Eligible Date : {1}" & vbNewLine, _
                                 x.Attribute("OfferID").Value, _
                                 x.Element("EligibleDate").Value)
               Return True
           End Function) _
    .Descendants("OfferProgressTotal") _
    .ToList _
    .ForEach(Sub(x)
                 Console.WriteLine("ID : {0}" & vbNewLine _
                                   & "Description : {1}" & vbNewLine _
                                   & "Lifetime Value : {2}" & vbNewLine, _
                                   x.Attribute("ID").Value, _
                                   x.Attribute("Description").Value, _
                                   x.Element("LifetimeValue").Value)
             End Sub)

This will output the info you showed for the parent node then the info for the childnodes that you said you wanted

commented: With a bit of massaging, this was the PERFECT answer to what I needed to do. Many thanks! +0
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.