Click here to Skip to main content
15,892,768 members
Articles / Web Development / ASP.NET
Tip/Trick

Dynamically append an XML document

Rate me:
Please Sign up or sign in to vote.
3.67/5 (3 votes)
23 Mar 2010CPOL 13.2K   2   2
This code demonstrates how to append an XML document. We will add a new element and some inner text. Call the function, provide arguments for the path to the file, the name of the child node, the element name, and the text to add. The file is opened, appended, and saved. The function returns a...
This code demonstrates how to append an XML document. We will add a new element and some inner text.
Call the function, provide arguments for the path to the file, the name of the child node, the element name, and the text to add. The file is opened, appended, and saved. The function returns a true if it succeeds and a false if it fails.

This is the before and after of the pageLinks.xml file.
before:
<linklist>
  <link>http://www.google.com</link>
</linklist>

after:
<linklist>
  <link>http://www.google.com</link>
  <link>http://www.yahoo.com</link>
</linklist>


I'm using the "button 2" click event to call the function...
Private Sub Button2_Click(ByVal sender As System.Object, _
    ByVal e As System.EventArgs) Handles Button2.Click
    Dim myFilePath = _
    "C:\pageLinks.xml"
    Dim mybool As Boolean = saveToXMLDoc("http://yahoo.com", _
    myFilePath, "linkList", "link")
End Sub


The function is called....

Private Function saveToXMLDoc(ByVal textToAdd As String, _
     ByVal filePath As String, ByVal xNode As String, _
     ByVal addedElement As String) As Boolean
    Dim success As Boolean
    Try
        'create place for the document in memory and load it with an existing doc
        Dim xmlLinksDoc As XmlDocument = New XmlDocument
        xmlLinksDoc.Load(filePath)
        Dim xmlNavigator As Xml.XPath.XPathNavigator = xmlLinksDoc.CreateNavigator
        xmlNavigator = xmlNavigator.SelectSingleNode(xNode)
        'xNode is the node we are adding a new child to
        'example: xmlNavigator = nav.SelectSingleNode("linkList")
        xmlNavigator.AppendChildElement("", addedElement, "", textToAdd)
        'adds the element (addElement) and the text (textToAdd)
        'example: xmlNavigator.AppendChildElement("", "textHolder", "", "This is the text that will be added to the 'textHold' node.")
        xmlLinksDoc.Save(filePath)
        success = True
    Catch ex As Exception
        success = False
    End Try
    Return success
End Function

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior) Jacobs Technology
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralHello I"m trying to append the entrys in this xml file in as... Pin
vince.vb4-Oct-11 8:41
vince.vb4-Oct-11 8:41 
GeneralHello I"m trying to append the entrys in this xml file in as... Pin
vince.vb4-Oct-11 8:40
vince.vb4-Oct-11 8:40 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.