Solved

Find a specific XML node with Vb.net

Posted on 2008-10-15
4
1,927 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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Since .Net 2.0, Visual Basic has made it easy to create a splash screen and set it via the "Splash Screen" drop down in the Project Properties.  A splash screen set in this manner is automatically created, displayed and closed by the framework itsel…
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, …
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

744 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

11 Experts available now in Live!

Get 1:1 Help Now