Class

XMLNode


Description

XMLNode is the base class for most of the XML Document Object Model (DOM) classes and implements many of the common properties and functions of a DOM node. This class won't usually be instantiated directly. Most objects will be created from methods of the XMLDocument class.

Properties

Name

Type

Read-Only

Shared

ChildCount

Integer

FirstChild

XMLNode

LastChild

XMLNode

LastError

Integer

LocalName

String

Name

String

NamespaceURI

String

NextSibling

XMLNode

OwnerDocument

XMLDocument

Parent

XMLNode

Prefix

String

PreviousSibling

XMLNode

Type

Integer

Value

String

Methods

Name

Parameters

Returns

Shared

AppendChild

NewChild As XMLNode

XMLNode

Child

Index As Integer

XMLNode

Clone

Deep As Boolean

XMLNode

Compare

NodeToCompare As XMLNode

Integer

GetAttribute

Name As String

String

GetAttributeNode

Name As String

XMLAttribute

Insert

NewChild As XMLNode, RefChild As XMLNode

XMLNode

RemoveAttribute

name As String

RemoveAttributeNode

attributeNode As XMLAttribute

XMLAttribute

RemoveChild

OldChild As XMLNode

ReplaceChild

NewChild As XMLNode, OldChild As XMLNode

XMLNode

SetAttribute

Name As String, Value As String

SetAttributeNode

AttributeNode As XMLAttribute, [ns As Boolean]

XMLAttribute

ToString

String

XQL

Query As String, [Map() As String]

XMLNodeList

Property descriptions


XMLNode.ChildCount

ChildCount As Integer

The number of child nodes contained by this node.

This property is read-only.

The example code below uses this XML. Assign it to 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>

This example displays the number of teams in the above XML:

Var xml As New XmlDocument(kXml)
MessageBox("Teams in League: " + xml.DocumentElement.ChildCount.ToString)

XMLNode.FirstChild

FirstChild As XMLNode

The first child of this node.

This property is read-only.

The example code below uses this XML. Assign it to 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>

This example displays the first Team in the League XML:

Var xml As New XmlDocument(kXML)

MessageBox("First Team in League: " + xml.DocumentElement.FirstChild.GetAttribute("name"))

XMLNode.LastChild

LastChild As XMLNode

The first child of this node.

This property is read-only.

The example code below uses this XML. Assign it to 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>

This example displays the last team in the League XML:

Var xml As New XmlDocument(kXML)

MessageBox("Last Team in League: " + xml.DocumentElement.LastChild.GetAttribute("name"))

XMLNode.LastError

LastError As Integer

Contains an error code after an XMLException occurs.

This property is read-only.

XMLNodeList.Item will specify an error code if the item could not be created, and XMLNodeList.ToString could as well.

Error Code

Description

0

No error occurred.

1

Index size error

2

DOM String size error

3

Hierarchy request error

4

Wrong document

5

Invalid character

6

No data allowed

7

No modification allowed

8

Not found

9

Not supported

10

In use attribute

11

Invalid state

12

Syntax error

13

Invalid modification

14

Namespace error

15

Invalid access

16

Invalid node type

17

Query parse error

18

Query execution error

19

Not OK


XMLNode.LocalName

LocalName As String

The name of the node without the prefix.

This property is read-only.

The example code below uses this XML. Assign it to 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>

This example displays the name of the root node in the XML:

Var xml As New XmlDocument(kXML)

MessageBox("LocalName of root node: " + xml.DocumentElement.LocalName) ' League

XMLNode.Name

Name As String

The name of this node.

The example code below uses this XML. Assign it to 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>

This example displays the name of the root node in the XML:

Var xml As New XmlDocument(kXML)

MessageBox("Name of root node: " + xml.DocumentElement.Name) ' League

This code changes the name of the root node from "League" to "AmericanLeague":

Var xml As New XmlDocument(kXML)
xml.DocumentElement.Name = "AmericanLeague"

MessageBox("Name of root node: " + xml.DocumentElement.Name) ' AmericanLeague

XMLNode.NamespaceURI

NamespaceURI As String

The namespaceURI of this node.

This property is read-only.


XMLNode.NextSibling

NextSibling As XMLNode

The next node.

This property is read-only.

The example code below uses this XML. Assign it to 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>

This example shows how to walk over the team nodes in the above XML and displays each team name:

Var xml As New XmlDocument(kXML)
Var n As XmlNode = xml.DocumentElement.FirstChild

While n <> Nil
  MessageBox(n.Name + ": " + n.GetAttribute("name"))
  n = n.NextSibling
Wend

XMLNode.OwnerDocument

OwnerDocument As XMLDocument

The XMLDocument that contains this node.

This property is read-only.

Use this property to get a reference to the XMLDocument that you can then use with the various "Create" methods to add new items to the XML. This is useful when you have access to an XMLNode, but no longer have a reference to the XMLDocument that contains it.


XMLNode.Parent

Parent As XMLNode

The parent of this node.

This property is read-only.

The example code below uses this XML. Assign it to 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>

This example displays the parent of the last team in the above XML:

Var xml As New XmlDocument(kXML)
Var n As XmlNode = xml.DocumentElement.LastChild

If n <> Nil Then
  MessageBox("Name of " + n.Name + " node: " + n.Parent.Name)
End If

XMLNode.Prefix

Prefix As String

The namespace prefix of this node.

This property is read-only.


XMLNode.PreviousSibling

PreviousSibling As XMLNode

The preceding node.

This property is read-only.

The example code below uses this XML. Assign it to 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>

This example shows how to walk in reverse over the team nodes in the above XML and displays each team name:

Var xml As New XmlDocument(kXML)
Var n As XmlNode = xml.DocumentElement.LastChild

While n <> Nil
  MessageBox(n.Name + ": " + n.GetAttribute("name"))
  n = n.PreviousSibling
Wend

XMLNode.Type

Type As Integer

Integer constant denoting the type, such as Element, Attribute, TextNode, and so forth.

This property is read-only.

Use the Class Constants of the XMLNodeType object to compare values.


XMLNode.Value

Value As String

Used in some nodes to set or get the value, such as XMLTextNode and XMLAttribute.

Not supported by all XMLNode subclasses.

Method descriptions


XMLNode.AppendChild

AppendChild(NewChild As XMLNode) As XMLNode

Adds a child after the last child.

Typically you will want to use the first syntax that returns an instance of the newly created child so that you can then attach information to the child.

The following XML:

<?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>

Can be created using this code, which displays the XML to a TextArea and prompts you to save it to a file:

Var xml As New XmlDocument

Var root As XmlNode
root = xml.AppendChild(xml.CreateElement("League"))

Var teamNode As XmlNode
Var playerNode As XmlNode

' Create 1st team and its players
teamNode = root.AppendChild(xml.CreateElement("Team"))
teamNode.SetAttribute("name", "Seagulls")

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

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

' Create 2nd team and its players
teamNode = root.AppendChild(xml.CreateElement("Team"))
teamNode.SetAttribute("name", "Pigeons")

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

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

' Create 3rd team and its players
teamNode = root.AppendChild(xml.CreateElement("Team"))
teamNode.SetAttribute("name", "Crows")

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

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

TextArea1.Text = xml.ToString

DisplayXML(xml)

XMLNode.Child

Child(Index As Integer) As XMLNode

Returns the child XMLNode at the position denoted by Index. Index is zero-based.

This example loads the following XML, contained in a constant called kTestXml, into a new XMLDocument and then displays the team names:

<?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>
' Load XML
Var xml As New XmlDocument
Try
  xml.LoadXml(kTestXml)
Catch e As XmlException
  MessageBox("XML error: " + e.Message)
  Return
End Try

For team As Integer = 0 To xml.DocumentElement.ChildCount-1
  MessageBox("Team: " + xml.DocumentElement.Child(team).GetAttribute("name"))
Next

XMLNode.Clone

Clone(Deep As Boolean) As XMLNode

Duplicates the current node. The Deep parameter indicates whether to also duplicate all the child nodes. It returns an XMLNode.

The example code below uses this XML. Assign it to 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>

Duplicate the first team node, change the team name and add it to the XML:

Var xml As New XmlDocument(kXML)

Var n As XmlNode = xml.DocumentElement.FirstChild

If n <> Nil Then
  ' Duplicate the first team
  Var dup As XmlNode = n.Clone(True)

  ' Change its name to "Eagles"
  dup.SetAttribute("name", "Eagles")

  ' Add it to the XmlDocument
  n.Parent.AppendChild(dup)

  TextArea1.Text = xml.ToString
End If

The result would then be:

<?xml version="1.0" encoding="UTF-8"?>
<League>
  <Team name="Eagles">
              <Player name="Bob" position="1B" />
              <Player name="Tom" position="2B" />
      </Team>
      <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>

XMLNode.Compare

Compare(NodeToCompare As XMLNode) As Integer

Compares two nodes, which must both have the same parent. Returns an Integer that works like String.Compare.

The two nodes are compared using their string contents.

  • If XMLNode < NodeToCompare it returns -1

  • If XMLNode = NodeToCompare it returns 0

  • If XMLNode > NodeToCompare it returns 1

If you need to compare nodes with different parents, use the String.Compare function on the ToString value.


XMLNode.GetAttribute

GetAttribute(Name As String) As String

Gets the value of the attribute specified by URI and Name.

Returns "" (empty string) if the attribute does not exist.

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>

This example shows how to walk over the team nodes in the above XML and displays each team name:

Var xml As New XmlDocument(kXML)
Var n As XmlNode = xml.DocumentElement.FirstChild

While n <> Nil
  MessageBox(n.Name + ": " + n.GetAttribute("name"))
  n = n.NextSibling
Wend

XMLNode.GetAttributeNode

GetAttributeNode(Name As String) As XMLAttribute

Gets an XML attribute node of the attribute specified by Index position. Index is zero-based.

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>

This example shows how to walk over the team nodes in the above XML and displays each team name:

Var xml As New XmlDocument(kXML)
Var n As XmlNode = xml.DocumentElement.FirstChild

Var an As XmlAttribute
While n <> Nil
  an = n.GetAttributeNode("name")
  MessageBox(n.Name + ": " + an.Value)

  n = n.NextSibling
Wend

XMLNode.Insert

Insert(NewChild As XMLNode, RefChild As XMLNode) As XMLNode

Inserts NewChild before the position of RefChild. It optionally returns a reference to the inserted node as an XMLNode.

The children being added must be children of the node/document to which they are being inserted. The example demonstrates this.

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>

To add a new team before the first team in the XML:

Var xml As New XmlDocument(kXml)

Var n1 As XmlNode = xml.DocumentElement.FirstChild

' Insert a new team before the first team currently
' in the XML
Var newTeam As XmlNode
newTeam = xml.DocumentElement.AppendChild(xml.CreateElement("Team"))
newTeam.SetAttribute("name", "Eagles")

xml.DocumentElement.Insert(newTeam, n1)

TextArea1.Text = xml.ToString

The result would then be:

<?xml version="1.0" encoding="UTF-8"?>
<League>
  <Team name="Eagles"/>
      <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>

XMLNode.RemoveAttribute

RemoveAttribute(name As String)

Removes the name attribute.


XMLNode.RemoveAttributeNode

RemoveAttributeNode(attributeNode As XMLAttribute) As XMLAttribute

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>

Remove the name attribute from the first Team:

Var xml As New XmlDocument(kXML)
Var n As XmlNode = xml.DocumentElement.FirstChild
Var an As XmlAttribute
an = n.GetAttributeNode("name")
n.RemoveAttributeNode(an)

TextArea1.Text = xml.ToString

The result would then be:

<?xml version="1.0" encoding="UTF-8"?>
<League>
      <Team>
              <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>

XMLNode.RemoveChild

RemoveChild(OldChild As XMLNode)

Removes OldChild from the XML.

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>

Remove the "Seagulls" team from the XML:

Var xml As New XmlDocument(kXML)

Var n As XmlNode = xml.DocumentElement.FirstChild
Call xml.DocumentElement.RemoveChild(n)

TextArea1.Text = xml.ToString

The result would then be:

<?xml version="1.0" encoding="UTF-8"?>
<League>
      <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>

XMLNode.ReplaceChild

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

Replaces oldChild with NewChild. ReplaceChild optionally returns a reference to the new child as an XMLNode.

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.Text = xml.ToString

The result would then be:

<?xml version="1.0" encoding="UTF-8"?>
<League>
  <Team name="Eagles">
    <Player name="Fred" position="1B"/>
    <Player name="Phil" 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>

XMLNode.SetAttribute

SetAttribute(Name As String, Value As String)

This syntax sets an attribute and declares a namespace.

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>

Add a "city" attribute to the first team:

Var xml As New XmlDocument(kXML)
Var n As XmlNode = xml.DocumentElement.FirstChild
n.SetAttribute("city", "Boston")

TextArea1.Text = xml.ToString

The result would then be:

<?xml version="1.0" encoding="UTF-8"?>
<League>
      <Team name="Seagulls" city="Boston">
              <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>

XMLNode.SetAttributeNode

SetAttributeNode(AttributeNode As XMLAttribute, [ns As Boolean]) As XMLAttribute

Sets an attribute node.

SetAttributeNode optionally returns a reference to a node as an XMLAttribute that had the same name and was replaced. The optional parameter ns determines whether to use and declare any namespace data found in the passed XMLAttribute.

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>

Adds a "city" attribute to the first team:

Var xml As New XmlDocument(kXML)

Var n As XmlNode = xml.DocumentElement.FirstChild

Var a As XmlAttribute
a = xml.CreateAttribute("city")
a.Value = "Boston"

n.SetAttributeNode(a)

TextArea1.Text = xml.ToString

The result would then be:

<?xml version="1.0" encoding="UTF-8"?>
<League>
      <Team name="Seagulls" city="Boston">
              <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>

XMLNode.ToString

ToString As String

A string representation of this node.

This property is read-only.

The example code below uses this XML. Assign it to 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>

Display the XML for the first team node:

Var xml As New XmlDocument(kXML)

Var n As XmlNode = xml.DocumentElement.FirstChild

If n <> Nil Then
  MessageBox("XML: " + n.ToString)
End If

XMLNode.XQL

XQL(Query As String, [Map() As String]) As XMLNodeList

Performs an XPath 1.0 query and returns an XMLNodeList of the resulting nodes.

If the query has namespace references in it, you must provide declarations for them in the form of a string array, such as:

Var map() As String = Array("ns1","http://foo","ns2","http://bar")

Xql is also called XPath. Version 1.0 of XPath are supported.

For more information about XPath:

The example code below uses this XML. Assign it to a constant called kTestXML:

<?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="Who" position="1B" />
              <Player name="What" position="2B" />
              <Player name="I Don't Know" position="3B" />
      </Team>
</League>

To get all the player names, you can use "//Player" as the query:

' Load XML
Var xml As New XmlDocument
Try
  xml.LoadXml(kTestXml)
Catch e As XmlException
  MessageBox("XML error: " + e.Message)
End Try

' Display all the Player names
Var nodes As XmlNodeList
nodes = xml.XQL("//Player") ' Find all Player nodes in XML

' Loop through results and display each name attribute
Var node As XmlNode
For i As Integer = 0 To nodes.Length - 1
  node = nodes.Item(i)
  MessageBox("Player: " + node.GetAttribute("name"))
Next

Notes

Note that not every property is utilized on every node type. For example, the Value property is used on an XMLAttribute and XMLTextNode, but not an XMLElement.

Sample code

The following XML:

<?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>

Can be created using this code, which displays the XML to a TextArea and prompts you to save it to a file:

Var xml As New XmlDocument

Var root As XmlNode
root = xml.AppendChild(xml.CreateElement("League"))

Var teamNode As XmlNode
Var playerNode As XmlNode

' Create 1st team and its players
teamNode = root.AppendChild(xml.CreateElement("Team"))
teamNode.SetAttribute("name", "Seagulls")

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

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

' Create 2nd team and its players
teamNode = root.AppendChild(xml.CreateElement("Team"))
teamNode.SetAttribute("name", "Pigeons")

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

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

' Create 3rd team and its players
teamNode = root.AppendChild(xml.CreateElement("Team"))
teamNode.SetAttribute("name", "Crows")

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

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

TextArea1.Text = xml.ToString

DisplayXML(xml)

This code iterates through the XML created above and displays it in a ListBox:

Sub DisplayXML(xml As XmlDocument)
  Var root As XmlNode
  root = xml.DocumentElement

  XMLList.DeleteAllRows

  Var teamNode As XmlNode
  Var playerNode As XmlNode
  For team As Integer = 0 To root.ChildCount-1

    ' Add Team name
    teamNode = root.Child(team)
    XMLList.AddRow(teamNode.GetAttribute("name"))

    ' Add Players
    For player As Integer = 0 To teamNode.ChildCount-1
      playerNode = teamNode.Child(player)
      XMLList.AddRow(playerNode.GetAttribute("name"), _
      playerNode.GetAttribute("position"))
    Next

  Next
End Sub

Compatibility

Desktop, console, web and iOS project types on all supported operating systems.

See also

Object parent class; XMLDocument, XMLElement, XMLNodeList classes.