Class

XMLDocument


Description

Use to represents an XML document.

Property descriptions


XMLDocument.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)

XMLDocument.DocumentElement

DocumentElement As XMLElement

Refers to the top-level (root) element of the document.

You will always start processing the XML document starting with the DocumentElement 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 number of teams in the above XML:

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

XMLDocument.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"))

XMLDocument.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"))

XMLDocument.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

Invalide 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


XMLDocument.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

XMLDocument.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

XMLDocument.NamespaceURI

NamespaceURI As String

The namespaceURI of this node.

This property is read-only.


XMLDocument.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

XMLDocument.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.


XMLDocument.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

XMLDocument.Prefix

Prefix As String

The namespace prefix of this node.

This property is read-only.


XMLDocument.PreserveWhiteSpace

PreserveWhiteSpace As Boolean

When False, white space characters (spaces, carriage returns, tabs, etc.) are removed from within XML elements when the XML file is loaded. The default is False.

When True, white space characters are retained. You can only change PreserveWhiteSpace to True before calling LoadXml. It has no affect when set after the XML file has been loaded.

As an example, the following XML section has a space within the <trans> node:

<trans loc="fr"> </trans>

When PreserveWhiteSpace is False, the XML gets loaded as:

<trans loc="fr"></trans>

When PreserveWhiteSpace is True, the space in the XML node is retained:

<trans loc="fr"> </trans>

Note that this can cause you to have to look at additional nodes that might otherwise have been blank.


XMLDocument.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

XMLDocument.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

XMLDocument.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.


XMLDocument.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


XMLDocument.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)

XMLDocument.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

XMLDocument.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

XMLDocument.Compare

Compare(NodeToCompare As XMLNode) As Integer

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

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 function on the XMLNode value.


XMLDocument.Constructor

Constructor

Note

Constructors are special methods called when you create an object with the New keyword and pass in the parameters above.

Creates an empty XMLDocument instance.


XMLDocument.Constructor

Constructor(XML as String)

Note

Constructors are special methods called when you create an object with the New keyword and pass in the parameters above.

Parses the passed XML string into the document.


XMLDocument.Constructor

Constructor(XML as String)

Note

Constructors are special methods called when you create an object with the New keyword and pass in the parameters above.

Parses the passed XML string into the document.

This example loads the following XML, contained in a constant called kXml, into a new XMLDocument:

<?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(kXml)
Catch e As XmlException
  MessageBox("XML error: " + e.Message)
End Try

XMLDocument.CreateAttribute

CreateAttribute(Name As String) As XMLAttribute

Creates an attribute node as an XMLAttribute with a namespace declaration.


XMLDocument.CreateAttribute

CreateAttribute(URI As String, Name As String) As XMLAttribute

Creates an attribute node as an XMLAttribute with a namespace declaration.

After you create an attribute, you have to add it to the XMLDocument using SetAttributeNode.

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 an attribute to the first team node (Seagulls):

Var xml As New XmlDocument(kXml)

// Create an attribute node and assign it a value
Var xa As XmlAttribute
xa = xml.CreateAttribute("TestAttribute")
xa.Value = "Test"

// Add the attribute to the XML document
xml.DocumentElement.FirstChild.SetAttributeNode(xa)

TextArea1.Text = xml.ToString

XMLDocument.CreateCDATASection

CreateCDATASection(Data As String) As XMLCDATASection

Creates a CDATA section with the passed Data and returns it as an XMLCDATASection.

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 CDATA section to the first team in the XML:

Var xml As New XmlDocument(kXml)

// Create a CDATA section node and assign it a value
Var data As String =  "<h1>Hello!</h1>"
Var xcdata As XmlCDATASection
xcdata = xml.CreateCDATASection(data)

// Add the CDATA section to the XML document
xml.DocumentElement.FirstChild.AppendChild(xcdata)

TextArea1.Text = xml.ToString

XMLDocument.CreateComment

CreateComment(Data As String) As XMLComment

Creates an XMLComment with the passed Data and returns it as an XMLComment.

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 comment to the first team node:

Var xml As New XmlDocument(kXml)

// Create a Comment and assign it a value
Var xc As XmlComment
xc = xml.CreateComment("TestComment")
xc.Value = "This node contains team information."

// Add the Comment to the XML document
xml.DocumentElement.FirstChild.AppendChild(xc)

TextArea1.Text = xml.ToString

XMLDocument.CreateElement

CreateElement(TagName As String) As XMLElement

Creates an element with a namespace declaration as an XMLElement. Returns an XMLElement.


XMLDocument.CreateElement

CreateElement(URI As String, Tagname As String) As XMLElement

Creates an element with a namespace declaration as an XMLElement. Returns an XMLElement.

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 to the XML:

Var xml As New XmlDocument(kXml)

// Add a new Team to the XML
Var xe As XmlElement
xe = xml.CreateElement("Team")
xe.SetAttribute("name", "Eagles")

// Add the Comment to the XML document
xml.DocumentElement.AppendChild(xe)

TextArea1.Text = xml.ToString

XMLDocument.CreateProcessingInstruction

CreateProcessingInstruction(Target As String, Data As String) As XMLProcessingInstruction

Creates an XMLProcessingInstruction with the passed target keyword and data.

A processing instruction is a node type intended to carry instructions to the application.

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 processing instruction:

Var xml As New XmlDocument(kXml)

// Add a new Processing Instruction to the XML
Var xpi As XmlProcessingInstruction
xpi = xml.CreateProcessingInstruction("Target", "Data")

// Add the Processing Instruction to the XML document
xml.AppendChild(xpi)

TextArea1.Text = xml.ToString

XMLDocument.CreateTextNode

CreateTextNode(Data As String) As XMLTextNode

Creates a text node with Data and returns it as an XMLTextNode.

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 text node to the first team node:

Var xml As New XmlDocument(kXml)

// Create a Text node and assign it a value
Var xt As XmlTextNode
xt = xml.CreateTextNode("Maine")

// Add the Text node to the XML document
xml.DocumentElement.FirstChild.AppendChild(xt)

TextArea1.Text = xml.ToString

XMLDocument.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

XMLDocument.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

XMLDocument.ImportNode

ImportNode(foreignNode As XMLNode, [deep As Boolean]) As XMLNode

Copies a node from another XMLDocument into the current document.

If the optional parameter deep is True, ImportNode will import all the child nodes of foreignNode. After you import a node, you must then place it somewhere within the XMLDocument.

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 copy the first team into a new XML document:

Var xml1 As New XmlDocument(kXml)
Var xml2 As New XmlDocument

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

Var importNode As XmlNode
importNode = xml2.ImportNode(xml1.DocumentElement.FirstChild, True)

root.AppendChild(importNode)

TextArea1.Text = xml2.ToString

XMLDocument.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

XMLDocument.LoadXML

LoadXML(Doc As String)

Parses the passed XML file into the document.


XMLDocument.LoadXML

LoadXML(f As FolderItem)

Parses the passed XML file into the document.

Always check for an XMLException when loading XML from strings or files.

This example loads the following XML, contained in a constant called kTestXml, into a new XMLDocument:

<?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)
End Try

This example prompts the user to choose an XML file to load:

Var xmlFile As FolderItem
xmlFile = FolderItem.ShowOpenFileDialog("")

If xmlFile <> Nil Then
  Var xml As New XmlDocument

  Try
    xml.LoadXml(xmlFile)
  Catch e As XmlException
    MessageBox("XML error: " + e.Message)
  End Try
End If

XMLDocument.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

XMLDocument.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

XMLDocument.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

XMLDocument.SaveXML

SaveXML(fItem As FolderItem)

Saves the XML to the passed FolderItem.

The following XML, stored in a constant called kXMLTest, is saved to a file selected by the user:

<?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>
Var saveFile As FolderItem
saveFile = FolderItem.ShowSaveFileDialog("", "Test.xml")

If saveFile <> Nil Then
  Var xml As New XmlDocument
  Try
    xml.LoadXml(kTestXml)
    xml.SaveXml(saveFile)
  Catch fileError As IOException
    MessageBox("Save error: " + FileError.Message)
  Catch XMLError As XmlException
    MessageBox("XML error: " + XMLError.Message)
  End Try
End If

XMLDocument.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

XMLDocument.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

XMLDocument.Transform

Transform(xsl As String) As String

Creates a new XML document that is the result of applying an XSLT stylesheet. Optionally register an event handler saxHandler to receive SAX style events on the output XML during the transformation.


XMLDocument.Transform

Transform(xsl As XMLStyleSheet, [saxHandler As XMLXsltHandler]) As String

Creates a new XML document that is the result of applying an XSLT stylesheet. Optionally register an event handler saxHandler to receive SAX style events on the output XML during the transformation.

Versions 1.0 of XSLT and XPath are supported. XSLT is described at: https://www.w3.org/TR/xslt/all/ and XPath at https://www.w3.org/TR/xpath/all/.

You can use XMLDocument.Transform to generate pretty-printed XML data using the following XSL.

<pre class="code"><?xml version="1.0" encoding="UTF-8"?> <xsl:transform version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:output method="xml" indent="yes" /> <xsl:template match="/">

<xsl:copy-of select="/" />

</xsl:template>

</xsl:transform></pre>

Save it in a project as a constant named kPrettyPrintXSL. Then the following code generates pretty-printed XML data from an XMLDocument object.

Var prettyXML As String = xml.Transform(kPrettyPrintXSL)

XMLDocument.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

An XMLDocument is an internal data structure that can be converted to a string using the ToString function. An XMLDocument is used for both parsing existing XML data into a DOM document structure or creating a new XML document from scratch.

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.RemoveAllRows

  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

All project types on all supported operating systems.