Avatar of meverts_dane
meverts_dane
 asked on

VBA - XML, selecting node values based on a variable

I have written the following code.
The code works if provide the search criteria as text, however I want to load it as a parameter that will be supplied at runtime.

 Dim paNode As XmlNode = node.SelectSingleNode("//Tags/Tag[@Name='Alarm'']")
I would like to figure out how to do something like this.
 Dim paNode As XmlNode = node.SelectSingleNode("//Tags/Tag[@Name='ParameterName'']")


Dim filename As String
        filename = "C:\share\MikesTestFile.xml"
        Dim tagAttName As String
        tagAttName = "Alarm"
        Dim doc As XmlDocument = New XmlDocument()
        doc.Load(filename)
 
        Dim nodeList As XmlNodeList = doc.SelectNodes("//Tag")
        Dim node As XmlNode
 
        For Each node In nodeList
            If node.Attributes("Name").Value.ToString = tagAttName Then
                ' removed node based on attribute.
 
                Dim paNode As XmlNode = node.SelectSingleNode("//Tags/Tag[@Name='Alarm'']")
                If paNode Is Nothing Then
                    MessageBox.Show("not working")
                End If
                node.ParentNode.RemoveChild(paNode)
 
                'Dim dspString As String = node.Attributes("Id").Value.ToString
                'MessageBox.Show("I found this ID value:" & dspString)
                'node.RemoveChild(node.SelectSingleNode("Genre"))
            End If
        Next
        doc.Save(filename)

Open in new window

XML.NET Programming

Avatar of undefined
Last Comment
meverts_dane

8/22/2022 - Mon
ASKER CERTIFIED SOLUTION
meverts_dane

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
All of life is about relationships, and EE has made a viirtual community a real community. It lifts everyone's boat
William Peck