Solved

Find a specific XML node with Vb.net

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

Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

Question has a verified solution.

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

Microsoft Reports are based on a report definition, which is an XML file that describes data and layout for the report, with a different extension. You can create a client-side report definition language (*.rdlc) file with Visual Studio, and build g…
It’s quite interesting for me as I worked with Excel using vb.net for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
There are cases when e.g. an IT administrator wants to have full access and view into selected mailboxes on Exchange server, directly from his own email account in Outlook or Outlook Web Access. This proves useful when for example administrator want…
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …

717 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