Solved

how to loop through this xml (so that i can put this in a db)

Posted on 2010-11-17
8
747 Views
Last Modified: 2013-11-10
i'm trying to loop through this xml which has nested nodes and attributes
and i'm a bit confused on how to do it successfully

one of the problems i'm having is that once i loop though one layer i'm getting doubles
Data.xml
0
Comment
Question by:pzirkind
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
8 Comments
 
LVL 3

Expert Comment

by:tomnorra
ID: 34157558
If you are trying to get it into a database why not just use a Stored Procedure to import the file? Assuming of course you are using SQL Server
0
 
LVL 5

Expert Comment

by:srinivasbn
ID: 34157771
Try to read the data to dataset using dataset.readxml. and then just have a look at the data using dataset visualizer. so that you may get some idea about the data. if not please provide me some code to look.
0
 

Author Comment

by:pzirkind
ID: 34158241
here is the code i wrote
the problem is that once i get to the second layer , i'm getting doubles


 Private Sub GetFirstItem(ByVal file_name As String)
        Label1.Text = ""

        Dim xmldoc As New XmlDocument
        xmldoc.Load(file_name)

        Dim nodelist As XmlNodeList = xmldoc.SelectNodes("Result/Product")
        Dim childnodelist As XmlNodeList = xmldoc.SelectNodes("Result/Product")
        Dim chidnodelist2 As XmlNodeList = xmldoc.SelectNodes("Result/Product/OtherData")
        Dim node As XmlNode
        Dim child As XmlNode



        For Each node In nodelist.Item(0)
            'Label1.Text = Label1.Text & node.Name & vbCrLf

            For Each child In childnodelist.Item(0)

                Label1.Text = Label1.Text & child.Name & vbCrLf
            Next
        Next

    End Sub
0
Revamp Your Training Process

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action.

 
LVL 83

Expert Comment

by:CodeCruiser
ID: 34158633
0
 
LVL 5

Expert Comment

by:srinivasbn
ID: 34158772
Hi have the code modify in the following manner

        Dim xmldoc As New Xml.XmlDocument
        xmldoc.Load(file_name)

        Dim nodelist As Xml.XmlNodeList = xmldoc.SelectNodes("Result/Product")
        Dim childnodelist As Xml.XmlNodeList = xmldoc.SelectNodes("Result/Product")
        Dim chidnodelist2 As Xml.XmlNodeList = xmldoc.SelectNodes("Result/Product/OtherData")
        Dim node As Xml.XmlNode
        Dim child As Xml.XmlNode

        For Each child In childnodelist.Item(0)
           Label1.Text = Label1.Text & " " & child.Name & vbCrLf
        Next


you just need to get the childnodes. Dont put that for loop in the product nodes loop. this will give only one time the result.

Srinivas.
0
 

Author Comment

by:pzirkind
ID: 34159016
the thing is that i need to grab both (and many more) levels of nodes
0
 
LVL 5

Accepted Solution

by:
srinivasbn earned 250 total points
ID: 34164609
Hi if you want to get the child nodes of a selected node then just have only with that. like the following you can get those.

        Dim xmldoc As New Xml.XmlDocument
        xmldoc.Load(file_name)

        Dim nodelist As Xml.XmlNodeList = xmldoc.SelectNodes("Result/Product")
        Dim childnodelist As Xml.XmlNodeList = xmldoc.SelectNodes("Result/Product")
        Dim chidnodelist2 As Xml.XmlNodeList = xmldoc.SelectNodes("Result/Product/OtherData")
        Dim node As Xml.XmlNode
        Dim child As Xml.XmlNode

        For Each child In childnodelist.Item(0)
           Label1.Text = Label1.Text & " " & child.Name & vbCrLf
        Next

       For Each child in childnodelist2.Item(0)
          Label2.Text = Label2.Text & " " & child.Name & vbCrLf
      Next

      For Each child in nodelist.Item(0)
         Label3.Text = Label3.Text & " " & child.Name & vbCrLf
      Next


Srinivas.
0
 

Author Comment

by:pzirkind
ID: 34164934
thanks a lot!

i appreciate you putting up with my slow brain
0

Featured Post

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

Question has a verified solution.

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

This is an explanation of a simple data model to help parse a JSON feed
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
An introduction to basic programming syntax in Java by creating a simple program. Viewers can follow the tutorial as they create their first class in Java. Definitions and explanations about each element are given to help prepare viewers for future …

751 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