Solved

Find a specific XML node with Vb.net

Posted on 2008-10-15
4
1,930 Views
Last Modified: 2012-05-05
If have the following xml structure in a file
<Profiles>
   <Profile USER="DEFAULT">
        <ANODE></ANODE>
        <BNODE></BNODE>
        <CNODE></CNODE>
    </Profile>
   <Profile USER="BOB">
        <ANODE></ANODE>
        <BNODE></BNODE>
        <CNODE></CNODE>
    </Profile>
</Profiles>

Is there a way to explicitly (and efficiently) return Bob's profile to an object or some structure that I can iterate through?  I'm totally new to reading XML in vb.net but I've found some stuff with XPATH that allows me to itterate through the document but I don't kinow how to just grab a node by value, or a set of nodes by parent node, filtering by node value.  Ultimately I want to grab the default profile, make changes to it and append it back to the bottom of the existing document with a new name. Help is greatly appreciated.  

0
Comment
Question by:jclemo
  • 2
4 Comments
 
LVL 7

Expert Comment

by:lakshmidurga
ID: 22728187
Dim XMLDoc As New XmlDocument
            Dim lRoot As XmlNode
            Dim lChildNode As XmlNode
            XMLDoc.Load(ApplicationFolderPath & "\Config.xml")
            lRoot = XMLDoc.DocumentElement
            For Each lChildNode In lRoot.ChildNodes
                Select Case lChildNode.Name
                      Case "SQLUserName"
                        lSQLUserName = lChildNode.InnerText
                    Case "SQLPassword"
                        lSQLPassword = lChildNode.InnerText
                    Case "SQLServerName"
                        lSQLServerName = lChildNode.InnerText
                    Case "SQLDatabaseName"
                        lSQLDatabaseName = lChildNode.InnerText
                End Select
            Next
        Catch ex As Exception
            LogError("Error in ReadConfig. Error : " & ex.ToString & "Source : " & ex.Source.ToString)
        End Try
    End Sub

You can parse thenode till BOB,and if matches BOB then try to get all the child elements of that.
0
 
LVL 7

Accepted Solution

by:
lakshmidurga earned 500 total points
ID: 22728205
In addition with the above code,when you reached BOB
 Dim lchildRoot As XmlNode
 Dim lgrandChildNode As XmlNode

lchildRoot =XMLDoc.DocumentElement
for each igrandchildnode in ichildroot.childnodes
Select Case igrandchildnode ..Name
case ANODE
  get data
case BNode
getdata
end select
next

add this code to parse BOB 's child node elements.
0
 
LVL 48

Expert Comment

by:jpaulino
ID: 22728983
I don't really don't understand what you want to but if it is to read information from the "BOB" profile, you can do this:
        Dim xmlDoc As New Xml.XmlDocument
        xmlDoc.Load("c:\myXml.xml")
        Dim node As Xml.XmlNode = xmlDoc.SelectSingleNode("//Profile[@USER='BOB']")
        If node IsNot Nothing Then
            For Each n As Xml.XmlNode In node.ChildNodes
                Debug.WriteLine("Node Name: " & n.Name)
                Debug.WriteLine("Node Text: " & n.InnerText)
            Next
        End If

Open in new window

0
 
LVL 13

Expert Comment

by:TechTiger007
ID: 22729451
You can start with some thing like this. Modify as your need

//structure to pass the values. You can use class or any other
class Values
public dim aNodeValue as string
public dim bNodeValue as string
public dim cNodeValue as string
End Class


//GetValues function takes input as USERNAME
dim values as Values = new Values()
Dim doc As New XmlDocument
        doc.Load(filepath)
        Dim usernode As XmlNode = doc.SelectSingleNode("/Profies/Profile[@USER='" & USERNAME & "']")
        If Not usernode Is Nothing Then
        values = new Values()
            dim aNodeValue as string
            dim bNodeValue as string
            dim cNodeValue as string

        values.aNodeValue =usernode.SelectSingleNode("ANODE").InnerText;
        values.bNodeValue = usernode.SelectSingleNode("BNODE").InnerText;
        values.cNodeValue = usernode.SelectSingleNode("CNODE").InnerText;
        End If

    GetValues = values
0

Featured Post

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

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

1.0 - Introduction Converting Visual Basic 6.0 (VB6) to Visual Basic 2008+ (VB.NET). If ever there was a subject full of murkiness and bad decisions, it is this one!   The first problem seems to be that people considering this task of converting…
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. 
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …
In a recent question (https://www.experts-exchange.com/questions/28997919/Pagination-in-Adobe-Acrobat.html) here at Experts Exchange, a member asked how to add page numbers to a PDF file using Adobe Acrobat XI Pro. This short video Micro Tutorial sh…

806 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