Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

XML Readn next record 1

Status
Not open for further replies.

Dashley

Programmer
Dec 5, 2002
925
US
I'm loading an xml document and pulling the data out. All is working fine. How do I get it tp itterate through or to the next record. I'm only sseing the same record on everything I've tried. There's 3k records in my file.
I've looked through some samels here but I always wind up staying on the same record.
Can soneone point me in the right direction.

Thanks!



Code:
   Dim XMLDealsDoc As New XmlDocument
        Dim xmlDealsrdr As XmlTextReader = New XmlTextReader("c:\deals.xml")
        xmlDealsrdr.WhitespaceHandling = WhitespaceHandling.None

        XMLDealsDoc.Load(xmlDealsrdr)

        Dim NSDealsManager As New XmlNamespaceManager(XMLDealsDoc.NameTable)
        NSDealsManager.AddNamespace("df", XMLDealsDoc.DocumentElement.NamespaceURI)
      

        Dim couponidSTR As String = Nothing
        Dim merchantidSTR As String = Nothing
        Dim merchantnameSTR As String = Nothing
        Dim networkSTR As String = Nothing
        Dim labelSTR As String = Nothing
        Dim restrictionsSTR As String = Nothing
        Dim couponcodeSTR As String = Nothing
        Dim linkSTR As String = Nothing
        Dim directlinkSTR As String = Nothing
        Dim startdateSTR As String = Nothing
        Dim enddateSTR As String = Nothing
        Dim imageSTR As String = Nothing
        Dim typeSTR As String = Nothing
        Dim categorySTR As String = Nothing
        Dim statusSTR As String = Nothing
        Dim lastupdatedSTR As String = Nothing
        Dim changeauditSTR As String = Nothing

        Dim couponid As XmlElement = DirectCast(XMLDealsDoc.SelectSingleNode("df:deals/df:item/df:couponid", NSDealsManager), XmlElement)
        Dim merchantid As XmlElement = DirectCast(XMLDealsDoc.SelectSingleNode("df:deals/df:item/df:merchantid", NSDealsManager), XmlElement)
        Dim merchantname As XmlElement = DirectCast(XMLDealsDoc.SelectSingleNode("df:deals/df:item/df:merchantname", NSDealsManager), XmlElement)
        Dim network As XmlElement = DirectCast(XMLDealsDoc.SelectSingleNode("df:deals/df:item/df:label", NSDealsManager), XmlElement)
        Dim label As XmlElement = DirectCast(XMLDealsDoc.SelectSingleNode("df:deals/df:item/df:merchantname", NSDealsManager), XmlElement)
        Dim restrictions As XmlElement = DirectCast(XMLDealsDoc.SelectSingleNode("df:deals/df:item/df:restrictions", NSDealsManager), XmlElement)
        Dim couponcode As XmlElement = DirectCast(XMLDealsDoc.SelectSingleNode("df:deals/df:item/df:couponcode", NSDealsManager), XmlElement)
        Dim link As XmlElement = DirectCast(XMLDealsDoc.SelectSingleNode("df:deals/df:item/df:link", NSDealsManager), XmlElement)
        Dim directlink As XmlElement = DirectCast(XMLDealsDoc.SelectSingleNode("df:deals/df:item/df:directlink", NSDealsManager), XmlElement)
        Dim startdate As XmlElement = DirectCast(XMLDealsDoc.SelectSingleNode("df:deals/df:item/df:startdate", NSDealsManager), XmlElement)
        Dim enddate As XmlElement = DirectCast(XMLDealsDoc.SelectSingleNode("df:deals/df:item/df:enddate", NSDealsManager), XmlElement)
        Dim image As XmlElement = DirectCast(XMLDealsDoc.SelectSingleNode("df:deals/df:item/df:image", NSDealsManager), XmlElement)
        Dim type As XmlElement = DirectCast(XMLDealsDoc.SelectSingleNode("df:deals/df:item/df:dealtypes/df:type", NSDealsManager), XmlElement)
        Dim category As XmlElement = DirectCast(XMLDealsDoc.SelectSingleNode("df:deals/df:item/df:categories/df:category", NSDealsManager), XmlElement)
        Dim status As XmlElement = DirectCast(XMLDealsDoc.SelectSingleNode("df:deals/df:item/df:status", NSDealsManager), XmlElement)
        Dim lastupdated As XmlElement = DirectCast(XMLDealsDoc.SelectSingleNode("df:deals/df:item/df:lastupdated", NSDealsManager), XmlElement)
        Dim changeaudit As XmlElement = DirectCast(XMLDealsDoc.SelectSingleNode("df:deals/df:item/df:changeaudit", NSDealsManager), XmlElement)



        couponidSTR = couponid.InnerText.ToString()
        merchantidSTR = merchantid.InnerText.ToString()
        merchantnameSTR = merchantname.InnerText.ToString()
        networkSTR = network.InnerText.ToString()
        labelSTR = label.InnerText.ToString()
        restrictionsSTR = restrictions.InnerText.ToString()
        couponcodeSTR = couponcode.InnerText.ToString()
        linkSTR = link.InnerText.ToString()
        directlinkSTR = directlink.InnerText.ToString()
        startdateSTR = startdate.InnerText.ToString()
        enddateSTR = enddate.InnerText.ToString()
        imageSTR = image.InnerText.ToString()
        typeSTR = type.InnerText.ToString()
        categorySTR = category.InnerText.ToString()
        statusSTR = status.InnerText.ToString()
        lastupdatedSTR = lastupdated.InnerText.ToString()
        changeauditSTR = changeaudit.InnerText.ToString()


        Response.Write(couponidSTR & "<br />")
        Response.Write(merchantidSTR & "<br />")
        Response.Write(merchantnameSTR & "<br />")
        Response.Write(networkSTR & "<br />")
        Response.Write(labelSTR & "<br />")
 
[0] You can use
[tt] .SelectNodes("df:deals/df:item", NSDealsManager)[/tt]
to get a nodelist of item and then iterate through all the item in the nodelist. From each item, use SelectSingleNode as you've done to each targetted child element.

[1] I can show you, however, a more or less equivalent approaching sticking to SelectSingleNode and use a do loop using NextSibling property for iteration.
[tt]
'etc etc
Dim item As XmlElement
Dim couponid As XmlElement 'put all the dim statement outside the loop
'etc etc for other targetted element

item=DirectCast(XMLDealsDoc.SelectSingleNode("/df:deals/df:item", NSDealsManager), XmlElement)
Do While (item IsNot Nothing)
If item.LocalName="item" then
couponid=DirectCast(item.SelectSingleNode("df:couponid", NSDealsManager)
'etc etc
'Response.Write etc etc
End If
item=item.NextSibling
Loop
[/tt]
[1.1] In contrast to starting with [0] approach, this one you have to test if the NextSibling not only is something, but its name be the desired "item" otherwise not to do anything with it.

[2] When you retrieve couponidSTR etc... you should better make sure couponid etc themselves are not nothing (there is no guarantee apriori each and every of those child elements is present within each item).

[3] I think once you call for InnerText, to further call for ToString() is pointless, but if you want to, you can!
 
Tsuji,

Thank you very much for responding and for you answer and tips.

The piece of the puzzle I was ultimatly missing was "item = item.NextSibling"

I've never iterrated thorugh multiple XML data befre. I've only worked with single responses requests.

I've got it working fine now thanks to you.

Take care
 
Thanks!

I take the opportunity to amend the incomplete line. The corresponding line should read like this, of course.
[tt] couponid=DirectCast(item.SelectSingleNode("df:couponid", NSDealsManager)[blue], XmlElement)[/blue][/tt]
 
Thanks, I completed it. Appreciate your writing back though.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top