|
Tip formerly from VB2TheMax
Expertise: Intermediate
Language: VB7
November 11, 2002
CreateAppendElement - appending a XmlElement under another XmlNode
' Create an XmlElement object with inner text and make it a child of another
' XmlNode.
' Note: requires Imports System.Xml
Function CreateAppendElement(ByVal parentNode As XmlNode, ByVal name As String, _
Optional ByVal innerText As String = Nothing) As XmlElement
' Create a new XmlElement object, set the return value.
Dim xmlEl As XmlElement = parentNode.OwnerDocument.CreateElement(name)
' Set its inner text
If Not (innerText Is Nothing) Then xmlEl.InnerText = innerText
' make it a child of its parent node.
parentNode.AppendChild(xmlEl)
' Return the new node to the caller
Return xmlEl
End Function
' Example:
' load a XML file
Dim xmldoc As New XmlDocument()
xmldoc.Load("Employees.xml")
' create new Employee element and set its attributes
Dim xmlEl As XmlElement = CreateAppendElement(xmldoc.DocumentElement, _
"Employee")
xmlEl.SetAttribute("id", "100")
' create sub-elements
CreateAppendElement(xmlEl, "firstName", "Joe")
CreateAppendElement(xmlEl, "lastName", "Doe")
' Note: This code is taken from Francesco Balena's
' "Programming Microsoft Visual Basic .NET" - MS Press 2002, ISBN 0735613753
' You can read a free chapter of the book at
' http://www.vb2themax.com/HtmlDoc.asp?Table=Books&ID=101000
Francesco Balena
If you have a hot tip and we publish it, we'll pay you. However, due to accounting overhead we no longer pay $10 for a single tip submission. You must accumulate 10 acceptable tips to receive payment. Be sure to include a clear explanation of what the technique does and why it's useful. If it includes code, limit it to 20 lines if possible. Submit your tip here.
|