Solved

Find a specific XML node with Vb.net

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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

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…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Are you ready to implement Active Directory best practices without reading 300+ pages? You're in luck. In this webinar hosted by Skyport Systems, you gain insight into Microsoft's latest comprehensive guide, with tips on the best and easiest way…

696 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