[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 231
  • Last Modified:

How do I edit this XML node? BIG POINTS!!

Using vb.net how do I update this XML?  I want to change the key of "001" to "002" and the value of "foo1" to "foo2"?

SAMPLE XML:      
<configuration>
     <appSettings>
          <add key="001" value="foo1" />
     </appSettings>
</configuration>


        I AM ALREADY ADDING, and DELETING (But I do not know how to UPDATE/EDIT the above):

        Dim configFile As String = "c:\config.xml"
        Dim xmlDoc As XmlDocument = New XmlDocument ' create a new XmlDocument object
        xmlDoc.Load(configFile) ' load the configuration file

        Dim appSettingsElement As XmlNode = xmlDoc.SelectSingleNode("//configuration/appSettings") ' get a reference to the <appSettings> section
        Dim newElement As XmlElement = xmlDoc.CreateElement("add") ' create a new <add> element

        newElement.SetAttribute("key", Trim(newKey.ToString))  ' set its key attribute
        newElement.SetAttribute("value", Trim(newKeyVal.ToString)) ' set its value attribute
        appSettingsElement.AppendChild(newElement) ' append it to the <appSettings> section

        xmlDoc.Save(configFile)
        xmlDoc = Nothing

       'TO DELETE I AM DOING:          myNode1.RemoveChild(myNode2), etc, etc... (In another routine...)

THANKS!

(URGENT)


0
afsanchez001
Asked:
afsanchez001
  • 3
1 Solution
 
nayernaguibCommented:
The following piece of code should do the job:

____________________________________________

        Dim doc As New XmlDocument
        Dim nodes As XmlNodeList
        Dim i As Integer
        doc.Load("c:\config.xml")
        nodes = doc.SelectNodes("//configuration/appSettings/add")
        For i = 0 To nodes.Count - 1
            If nodes(i).Attributes("key").Value = "001" And nodes(i).Attributes("value").Value = "foo1" Then
                nodes(i).Attributes("key").Value = "003"
                nodes(i).Attributes("value").Value = "foo3"
            End If
        Next
        doc.Save("c:\config.xml")

____________________________________________

_______________

  Nayer Naguib
0
 
afsanchez001Author Commented:
I have not had a chance to test it but will let you know ASAP!
0
 
nehayaCommented:
This is a confgiration file!
use UserScope setting file and you can change it throu the proxy class!
0
 
afsanchez001Author Commented:
nehaya, can you please show an example?  Thank you.
0
 
afsanchez001Author Commented:
nayernaguib, Thank you!  Excellent answer.  : )
0

Featured Post

Industry Leaders: 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!

  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now