XMLNode.ReplaceChild

From Xojo Documentation

Method

XMLNode.ReplaceChild(NewChild as XMLNode, OldChild as XMLNode) As XMLNode

Supported for all project types and targets.

Replaces oldChild with NewChild.

ReplaceChild optionally returns a reference to the new child as an XMLNode.

Example

The following XML is stored in a constant called kXML:

 <?xml version="1.0" encoding="UTF-8"?>
 <League>
 	<Team name="Seagulls">
 		<Player name="Bob" position="1B" />
 		<Player name="Tom" position="2B" />
 	</Team>
 	<Team name="Pigeons">
 		<Player name="Bill" position="1B" />
 		<Player name="Tim" position="2B" />
 	</Team>
 	<Team name="Crows">
 		<Player name="Ben" position="1B" />
 		<Player name="Ty" position="2B" />
 	</Team>
 </League>

Replaces the "Seagulls" with a new team "Eagles":

Var xml As New XmlDocument(kXML)

Var n As XmlNode = xml.DocumentElement.FirstChild

Var teamNode As XmlNode
Var playerNode As XmlNode

// Create a new team
teamNode = xml.DocumentElement.AppendChild(xml.CreateElement("Team"))
teamNode.SetAttribute("name", "Eagles")

playerNode = teamNode.AppendChild(xml.CreateElement("Player"))
playerNode.SetAttribute("name", "Fred")
playerNode.SetAttribute("position", "1B")

playerNode = teamNode.AppendChild(xml.CreateElement("Player"))
playerNode.SetAttribute("name", "Phil")
playerNode.SetAttribute("position", "2B")

// Replace the "Seagulls" with the "Eagles"
xml.DocumentElement.ReplaceChild(teamNode, n)

TextArea1.Value = xml.ToString