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

Posted on 2006-04-10
Last Modified: 2013-11-13
Using how do I update this XML?  I want to change the key of "001" to "002" and the value of "foo1" to "foo2"?

          <add key="001" value="foo1" />

        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 = Nothing

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



Question by:afsanchez001
    LVL 14

    Accepted Solution

    The following piece of code should do the job:


            Dim doc As New XmlDocument
            Dim nodes As XmlNodeList
            Dim i As Integer
            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



      Nayer Naguib

    Author Comment

    I have not had a chance to test it but will let you know ASAP!
    LVL 6

    Expert Comment

    This is a confgiration file!
    use UserScope setting file and you can change it throu the proxy class!

    Author Comment

    nehaya, can you please show an example?  Thank you.

    Author Comment

    nayernaguib, Thank you!  Excellent answer.  : )

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Maximize Your Threat Intelligence Reporting

    Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

    This article is meant to give a basic understanding of how to use R Sweave as a way to merge LaTeX and R code seamlessly into one presentable document.
    A short article about a problem I had getting the GPS LocationListener working.
    An introduction to basic programming syntax in Java by creating a simple program. Viewers can follow the tutorial as they create their first class in Java. Definitions and explanations about each element are given to help prepare viewers for future …
    In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…

    779 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

    Need Help in Real-Time?

    Connect with top rated Experts

    11 Experts available now in Live!

    Get 1:1 Help Now