

LINK: https://gambas-playground.proko.eu/?gist=a54770310663b48cb35aec0e458cf0c5

Use "gb.xml"

(BEISPIEL1) Ein neues Element erzeugen
(BEISPIEL2) Ein Attribut erzeugen und dieses einem Element an einer bestimmten Stelle hinzufügen
(BEISPIEL3) Text als TextContent erzeugen und einem Element an einer bestimmten Stelle hinzufügen

Dim contents As String = "<xml>"
"<foo>"
"<p attr=\"1\">Hello</p>"
"<p attr=\"2\">Hi</p>"
"<p attr=\"3\">Greetings</p>"
"</foo>"
"</xml>"

Dim element, parent As XmlElement
Dim newElement As XmlElement 'create a new element
Dim newElement2 As XmlElement 'create another new element
Dim doc As New XmlDocument

(BEISPIEL1)
newElement As New XmlElement("alpha")
newElement2 As New XmlElement("beta")

doc.FromString(contents)

(BEISPIEL2)
'Set some properties
newElement.Attributes["href"] = "https://gambas-playground.proko.eu"
newElement.TextContent = "The Gambas Playground !"
newElement2.TextContent = "Heya !"

element = doc.GetElementsByTagName("p")[1] 'Get the second p element
element.AppendChild(newElement)

parent = element.Parent 'Get the foo parent element
parent.InsertAfter(element, newElement2) 'Insert the new element right after the second p | Setzen Sie das neue Element direkt nach dem zweiten p ein
'Can be chained : element.Parent.InsertAfter(element, newElement2)
'Also available : parent.InsertBefore(element, newElement2) ' Setzen Sie das neue Element direkt vor einem (existierenden) Element ein

Print doc.ToString(True)

