Solved

Iterating xml node

Posted on 2011-02-16
12
1,110 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
The New “Normal” in Modern Enterprise Operations

DevOps for the modern enterprise offers many benefits — increased agility, productivity, and more, but digital transformation isn’t easy, especially if you’re not addressing the right issues. Register for the webinar to dive into the “new normal” for enterprise modern ops.

 

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

Webinar: Aligning, Automating, Winning

Join Dan Russo, Senior Manager of Operations Intelligence, for an in-depth discussion on how Dealertrack, leading provider of integrated digital solutions for the automotive industry, transformed their DevOps processes to increase collaboration and move with greater velocity.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Error message when using PrintSocket.Send 7 19
Groupbox Control ? 2 19
Help with a Python script converting xml to csv 4 23
Need a modeling tool 2 18
Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
The Confluence of Individual Knowledge and the Collective Intelligence At this writing (summer 2013) the term API (http://dictionary.reference.com/browse/API?s=t) has made its way into the popular lexicon of the English language.  A few years ago, …
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…

821 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