Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Find a specific XML node with Vb.net

Posted on 2008-10-15
4
Medium Priority
?
1,935 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 1500 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
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…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Suggested Courses

610 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