Solved

Iterating xml node

Posted on 2011-02-16
12
1,107 Views
Last Modified: 2012-05-11
I am trying to parse thru this xml. I am having a problem in iterating through multple "adins" node. (See below example) I am not able to go to the next "adins" node. The "adins" occurs multiple times. I am able to use the nextsibling property. But how can I go to the 3rd. Below is the code
Thank you for the help.
 
I am using in Visual basic 2010 (.net).
    xmlNode = xmlDoc.GetElementsByTagName("adins")
            singlexmlNode = xmlNode(I)
            Dim xmlInsertNode1 As XmlNode
            Dim xmlInsertNode2 As XmlNode
            k = 0
            For Each xmlInsertNode1 In singlexmlNode.ChildNodes
                If xmlInsertNode1.HasChildNodes Then
                    For Each xmlInsertNode2 In xmlInsertNode1.ChildNodes
                          MsgBox("cost: " & xmlInsertNode2.ChildNodes.Item(1).InnerText.Trim())
                         MsgBox("DATE: " & xmlInsertNode2.ChildNodes.Item(0).InnerText.Trim())
                        End If
                      ' Tried using child nodes but doesn't work.
                        drCurrent("date") = xmlNode(I).ChildNodes.Item(0).InnerText.Trim()
                        drCurrent("price") = xmlNode(I).ChildNodes.Item(1).InnerText.Trim()
                        'code to update data base
                    Next
                End If
           next
 
 
   'example layout of xml
 <Orders>
 <ord>
     <item1>sval 1</item1>
     <item2>sval 1</item2>
     <item3>sval 1</item3>
     <Accountinfo>
            <CustomerNumber>10059</CustomerNumber>
            <Name>test name</Name>
            <Company>company test</Company>
            <Contact>
               <PhoneNumber>222-2222</PhoneNumber>
               <BillingPhoneNumber>333-533-3333</BillingPhoneNumber>
               <Street>test add</Street>
            </Contact>
      </Accountinfo>
      <adins>
            <date>2011-01-01</date>
             <adcost>2200.00</adcost>
             <status>Active</status>
      </adins>
       <adins>
             <date>2011-01-02</date>
              <adcost>3200.00</adcost>
              <status>Active</status>
         </adins>
         <adins>
             <date>2011-01-03</date>
              <adcost>4200.00</adcost>
              <status>Active</status>
         </adins>
    </ord>
0
Comment
Question by:ssj1
  • 5
  • 5
  • 2
12 Comments
 
LVL 22

Expert Comment

by:pivar
ID: 34912029
Hi,

How about this?


/peter

For Each node As XmlNode In doc.SelectNodes("/Orders/ord/adins")
	For Each subnode As XmlNode In node.ChildNodes
		If subnode.Name.Equals("date") Then
			drCurrent("date") = subnode.InnerText
		End If
		If subnode.Name.Equals("adcost") Then
			drCurrent("price") = subnode.InnerText
		End If
		If subnode.Name.Equals("Status") Then
			drCurrent("status") = subnode.InnerText
		End If
	Next
Next

Open in new window

0
 

Author Comment

by:ssj1
ID: 34917531
Hello,
Thank you for the comments. It is helpfull.
It still goes thru only one set of "adins" nodes and sub nodes. It doesnt go to the next at all.
I dont know what I need to change in "            For Each node In singlexmlNode"

I tried all. I am new to xml scrirpting. Please please help!!!! Thank you


     
Dim xmlDoc As New XmlDocument
        Dim xmlNode As XmlNodeList
        Dim singlexmlNode As XmlNode
        Dim fs As New FileStream("C:\orders0206.xml", FileMode.Open, FileAccess.Read)
        xmlDoc.Load(fs)
        xmlNode = xmlDoc.SelectNodes("/Orders/ord/adins")
        singlexmlNode = xmlNode(I) 

         ' "I" for the current iteration thru which I get other fields using xml dataset 

            For Each node In singlexmlNode

                For Each subnode In node.ChildNodes

                        MsgBox(" chk " & subnode.InnerText)

                        If subnode.Name.Equals("date") Then
                            drCurrent("date") = subnode.InnerText
                        End If
                        If subnode.Name.Equals("adcost") Then
                         drCurrent("price") = subnode.InnerText
                Next
            Next \
' adins loop comes out after this...

Open in new window

0
 
LVL 22

Expert Comment

by:pivar
ID: 34920755
Why do you change

For Each node As XmlNode In doc.SelectNodes("/Orders/ord/adins")

to
 
xmlNode = xmlDoc.SelectNodes("/Orders/ord/adins")
singlexmlNode = xmlNode(I)

For Each node In singlexmlNode

that will only take the first xmlnode.

Please try my solution

Dim xmlDoc As New XmlDocument
        Dim xmlNode As XmlNodeList
        Dim fs As New FileStream("C:\orders0206.xml", FileMode.Open, FileAccess.Read)
        xmlDoc.Load(fs)
        xmlNode = xmlDoc.SelectNodes("/Orders/ord/adins")

         ' "I" for the current iteration thru which I get other fields using xml dataset 

            For Each node In xmlNode

                For Each subnode In node.ChildNodes

                        MsgBox(" chk " & subnode.InnerText)

                        If subnode.Name.Equals("date") Then
                            drCurrent("date") = subnode.InnerText
                        End If
                        If subnode.Name.Equals("adcost") Then
                         drCurrent("price") = subnode.InnerText
                Next
            Next \
' adins loop comes out after this...

Open in new window

0
 

Author Comment

by:ssj1
ID: 34921404
Pivar:
Thank you for the help. It goes through the complete loop. Basically it goes through all adins nodes for all the orders (not for that specific order). That's why I ended up doing with (I).  I need  <adins> nodes only for the particlar order number for each order.
For each order there will be one or multiple (3 to 10)  <adins> nodes.

Please help. I really need this. Thanks a lot again.
0
 
LVL 22

Expert Comment

by:pivar
ID: 34921455
Now I dont understand. You have 3 adin childnodes for the specified "ord" node. Dont you want all 3 childnodes?
0
 

Author Comment

by:ssj1
ID: 34921619
Yes, I need all adins nodes pertaining  to that order <ord>.  

(ie) One <ord> that is one order will have multple ads. If I use the code you suggested, it goes through all the <adins>nodes in the entire xml file. It doesnt stop when the <ord> order number changes.

Thank you please help
0
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 
LVL 22

Expert Comment

by:pivar
ID: 34921719
OK, I think I understand.

How about this? This will select the first occurence of ord (you could change this by changing order in [1])
Dim xmlDoc As New XmlDocument
        Dim xmlNode As XmlNodeList
        Dim fs As New FileStream("C:\orders0206.xml", FileMode.Open, FileAccess.Read)
        xmlDoc.Load(fs)
        xmlNode = xmlDoc.SelectNodes("/Orders/ord[1]/adins")

         ' "I" for the current iteration thru which I get other fields using xml dataset 

            For Each node In xmlNode

                For Each subnode In node.ChildNodes

                        MsgBox(" chk " & subnode.InnerText)

                        If subnode.Name.Equals("date") Then
                            drCurrent("date") = subnode.InnerText
                        End If
                        If subnode.Name.Equals("adcost") Then
                         drCurrent("price") = subnode.InnerText
                Next
            Next \
' adins loop comes out after this...

Open in new window

0
 

Author Comment

by:ssj1
ID: 34921897
No, it doesnt go to the next adins within  that <ORD>. It picks up the first node only. After the loop it comes out, as though only one <adins> is present.

Please help. Sorry to be a pain.....
0
 
LVL 22

Accepted Solution

by:
pivar earned 500 total points
ID: 34921987
Please try attached code. It works well for me. Have I got the correct xml structure?
Imports System
Imports System.Xml
Imports System.IO

Module Module1

    Sub Main()
        Dim doc As New XmlDocument()
        doc.LoadXml("<Orders>" & vbCr & vbLf & " <ord>" & vbCr & vbLf & "     <item1>sval 1</item1> " & vbCr & vbLf & "     <item2>sval 1</item2> " & vbCr & vbLf & "     <item3>sval 1</item3> " & vbCr & vbLf & "     <Accountinfo>" & vbCr & vbLf & "            <CustomerNumber>10059</CustomerNumber> " & vbCr & vbLf & "            <Name>test name</Name> " & vbCr & vbLf & "            <Company>company test</Company> " & vbCr & vbLf & "            <Contact>" & vbCr & vbLf & "               <PhoneNumber>222-2222</PhoneNumber> " & vbCr & vbLf & "               <BillingPhoneNumber>333-533-3333</BillingPhoneNumber> " & vbCr & vbLf & "               <Street>test add</Street> " & vbCr & vbLf & "            </Contact>" & vbCr & vbLf & "      </Accountinfo>" & vbCr & vbLf & "      <adins>" & vbCr & vbLf & "            <date>2011-01-01</date> " & vbCr & vbLf & "             <adcost>2200.00</adcost> " & vbCr & vbLf & "             <status>Active</status> " & vbCr & vbLf & "      </adins>" & vbCr & vbLf & "       <adins>" & vbCr & vbLf & "             <date>2011-01-02</date> " & vbCr & vbLf & "              <adcost>3200.00</adcost> " & vbCr & vbLf & "              <status>Active</status> " & vbCr & vbLf & "         </adins>" & vbCr & vbLf & "         <adins>" & vbCr & vbLf & "             <date>2011-01-03</date> " & vbCr & vbLf & "              <adcost>4200.00</adcost> " & vbCr & vbLf & "              <status>Active</status> " & vbCr & vbLf & "         </adins>" & vbCr & vbLf & "    </ord>" & vbCr & vbLf & " <ord>" & vbCr & vbLf & "     <item1>sval 1</item1> " & vbCr & vbLf & "     <item2>sval 1</item2> " & vbCr & vbLf & "     <item3>sval 1</item3> " & vbCr & vbLf & "     <Accountinfo>" & vbCr & vbLf & "            <CustomerNumber>10059</CustomerNumber> " & vbCr & vbLf & "            <Name>test name</Name> " & vbCr & vbLf & "            <Company>company test</Company> " & vbCr & vbLf & "            <Contact>" & vbCr & vbLf & "               <PhoneNumber>222-2222</PhoneNumber> " & vbCr & vbLf & "               <BillingPhoneNumber>333-533-3333</BillingPhoneNumber> " & vbCr & vbLf & "               <Street>test add</Street> " & vbCr & vbLf & "            </Contact>" & vbCr & vbLf & "      </Accountinfo>" & vbCr & vbLf & "      <adins>" & vbCr & vbLf & "            <date>2011-02-01</date> " & vbCr & vbLf & "             <adcost>2200.00</adcost> " & vbCr & vbLf & "             <status>Active</status> " & vbCr & vbLf & "      </adins>" & vbCr & vbLf & "       <adins>" & vbCr & vbLf & "             <date>2011-02-02</date> " & vbCr & vbLf & "              <adcost>3200.00</adcost> " & vbCr & vbLf & "              <status>Active</status> " & vbCr & vbLf & "         </adins>" & vbCr & vbLf & "         <adins>" & vbCr & vbLf & "             <date>2011-02-03</date> " & vbCr & vbLf & "              <adcost>4200.00</adcost> " & vbCr & vbLf & "              <status>Active</status> " & vbCr & vbLf & "         </adins>" & vbCr & vbLf & "    </ord>" & vbCr & vbLf & "</Orders>")


        Dim nodes As XmlNodeList = doc.SelectNodes("/Orders/ord[1]/adins")

        For Each node As XmlNode In nodes
            For Each subnode As XmlNode In node.ChildNodes
                Console.WriteLine(String.Format("{0}={1}", subnode.Name, subnode.InnerText))
            Next
        Next

        Console.ReadKey()
    End Sub
End Module

Open in new window

0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 34926478
It seems to me you just need one more loop to loop through the "adins" nodes you grabbed and stored in xmlNode:
xmlNode = xmlDoc.GetElementsByTagName("adins")

For Each adinNode As XmlNode In xmlNode
	For Each xmlInsertNode1 In adinNode.ChildNodes
		For Each xmlInsertNode2 In xmlInsertNode1.ChildNodes
			MsgBox("cost: " & xmlInsertNode2.InnerText.Trim())
			MsgBox("DATE: " & xmlInsertNode2.InnerText.Trim())
		Next
	Next
Next

Open in new window

0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 34926545
Isn't this a duplicate of the question you already have open here:  http://www.experts-exchange.com/Programming/Languages/.NET/Visual_Studio_.NET_2005/Q_26826303.html ?
0
 

Author Comment

by:ssj1
ID: 34931329
Pivar:
Thank you It worked. I tested it now.  It worked in the test xml and also in the actual xml.
the problem was I thought it the iteration starts from 0, but actually it starts at 1.

The other code I use with xml data set starts at 0.  
thank you for you help

0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
Many times as a report developer I've been asked to display normalized data such as three rows with values Jack, Joe, and Bob as a single comma-separated string such as 'Jack, Joe, Bob', and vice versa.  Here's how to do it. 
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

914 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now