Solved

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

Posted on 2010-11-17
8
730 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
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
 
LVL 83

Expert Comment

by:CodeCruiser
ID: 34158633
0
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.

 
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

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

Does the idea of dealing with bits scare or confuse you? Does it seem like a waste of time in an age where we all have terabytes of storage? If so, you're missing out on one of the core tools in every professional programmer's toolbox. Learn how to …
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
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 …
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…

867 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

21 Experts available now in Live!

Get 1:1 Help Now