Solved

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

Posted on 2010-11-17
8
724 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
Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

 
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Whether you've completed a degree in computer sciences or you're a self-taught programmer, writing your first lines of code in the real world is always a challenge. Here are some of the most common pitfalls for new programmers.
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

758 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

17 Experts available now in Live!

Get 1:1 Help Now