在VB.net中读取重复的XML节点

时间:2014-05-07 15:21:48

标签: xml vb.net nested

我有一个XML字符串,我需要拆除和处理。很抱歉这可能是一个简单的问题,但我是XML处理的新手。

以下是XML示例:

<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>

我可以使用此代码达到这个水平:

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

正如您在XML中看到的那样,有重复的节点,但我只想使用OfferID与我要查找的匹配的节点。我一直在寻找几天的例子,并没有想出任何东西。

谢谢!

2 个答案:

答案 0 :(得分:0)

查看XMLReaders(请参阅下面的链接)。应该很容易更改以下内容以满足您的需求:

http://msdn.microsoft.com/en-us/library/cc189056%28v=vs.95%29.aspx

答案 1 :(得分:0)

您的代码似乎按原样运行。但是,您的示例不包含“OfferProgressTotals / OfferID”

的任何重复值

如果需要匹配ID中的模式,它将存储为字符串,因此,您可以使用String的可用方法查找字符串开头的匹配项(.StartsWith),字符串的结尾( .EndsWith)或字符串中的任何子字符串(.Contains)。

如果您想要更简洁的东西,可以使用LINQ。像这样:

    Dim searchstr as String = "90"
    doc.Descendants("OfferProgressTotals") _
        .Where(Function(x) x.HasAttributes) _
        .Where(Function(x) x.Attribute("OfferID").Value.StartsWith(searchstr)) _
        .ToList _
        .ForEach(Sub(x) Console.WriteLine("Offer ID : " + x.Attribute("OfferID").Value & vbNewLine & "Eligible Date : " + x.Element("EligibleDate").Value))

这将输出以“90”开头的每个OfferID的信息。

如果您想要来自子节点的信息,这样的事情应该起作用:

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)

这会找到您正在查找的父节点并输出您想要的信息,然后它会遍历“OfferProgressTotal”的每个子节点并输出其信息。