# Programming XML Documents

> Get, set, iterate, insert and delete XML elements in Visual Basic with the XMLElements collection, XMLElement.Text, Add and Remove under the rules of a DTD.

- **Product:** TX Text Control ActiveX
- **Section:** Documentation
- **Kind:** Topic
- **Path:** TX Text Control ActiveX > Document Editor > Working With XML Files > Programming XML Documents
- **2167 words**
- **Web URL:** https://docs.textcontrol.com/tx-text-control-activex/document-editor/xml/programming/
- **LLMs URL:** https://docs.textcontrol.com/tx-text-control-activex/document-editor/xml/programming/llms.txt
- **LLMs-Full URL:** https://docs.textcontrol.com/tx-text-control-activex/document-editor/xml/programming/llms-full.txt

---

An application reads and changes a loaded XML document through its elements. `TXTextControl` exposes them as collections of [XMLElement](https://docs.textcontrol.com/tx-text-control-activex/reference/xmlelement/llms-full.txt) objects, one collection for each kind of element, and each collection can also insert and delete elements within the rules of the document's DTD.

The Visual Basic examples below work with the address list of [Editing XML Files](https://docs.textcontrol.com/tx-text-control-activex/document-editor/xml/editing/llms-full.txt), loaded into a `TXTextControl` named `TXTextControl1`.

### Get the Elements of One Kind

The [XMLElements](https://docs.textcontrol.com/tx-text-control-activex/reference/txtextcontrol/xmlelements-property/llms-full.txt) property returns an [XMLElements](https://docs.textcontrol.com/tx-text-control-activex/reference/xmlelements/llms-full.txt) collection with all elements of the same tag name. In the example document there is one collection for all *Address* elements and another one for all *Name* elements. The following line of code gets the collection of the *Name* elements:

```vbscript
Set NameElements = TXTextControl1.XMLElements("Name")
```

### Read the Text of an Element

A collection has the standard [Item](https://docs.textcontrol.com/tx-text-control-activex/reference/xmlelements/item-method/llms-full.txt) method, which returns a single element. The [Text](https://docs.textcontrol.com/tx-text-control-activex/reference/xmlelement/text-property/llms-full.txt) property of the `XMLElement` object returns its text. The following line of code displays the content of the second *Name* element:

```vbscript
MsgBox TXTextControl1.XMLElements("Name").Item(2).Text
```

`Item` is the default method of the `XMLElements` collection, so the following line of code is equivalent:

```vbscript
MsgBox TXTextControl1.XMLElements("Name")(2)
```

### Read and Set the Text of Elements With Sub-Elements

The [Text](https://docs.textcontrol.com/tx-text-control-activex/reference/xmlelement/text-property/llms-full.txt) property of an element that contains sub-elements is the complete text of all sub-elements, separated by `Chr(10)` characters. For the second *Address* element of the example document, `TXTextControl1.XMLElements("Address")(2)`, it displays:

```text
Tom Baker
Miller Street
Los Angeles
```

To set a new text with the `Text` property, specify a string whose parts are separated by `Chr(10)` characters:

```vbscript
TXTextControl1.XMLElements("Address")(2) = _
   "Paul Baskerville" & Chr(10) & "Jackson Street" & Chr(10) & "London"
```

### Iterate Through a Collection

With a `For Each` statement, a program iterates through an XML collection. The following example displays all names:

```vbscript
For Each element in TXTextControl1.XMLElements("Name")
   MsgBox element.Text
Next
```

For development environments that don't support `For Each` processing, the following code is equivalent. It uses the [Count](https://docs.textcontrol.com/tx-text-control-activex/reference/xmlelements/count-property/llms-full.txt) property of the collection:

```vbscript
Set elements = TXTextControl1.XMLElements("Name")
For i = 1 To elements.Count
   MsgBox elements(i).Text
Next i
```

### How the DTD Controls Insertions and Deletions

An `XMLElements` collection also has an [Add](https://docs.textcontrol.com/tx-text-control-activex/reference/xmlelements/add-method/llms-full.txt) and a [Remove](https://docs.textcontrol.com/tx-text-control-activex/reference/xmlelements/remove-method/llms-full.txt) method, which insert and delete elements. What they may insert or delete is decided by the structure of the document.

The structure of an XML document is described by a document type definition (DTD), which is either contained inline in the XML file or stored in a separate DTD file. The following DTD file, *addresses.dtd*, belongs to the example document:

```xml
<!ELEMENT Addresses (DocumentHeadline, AddressTable)>
<!ELEMENT DocumentHeadline (#PCDATA)>
<!ELEMENT AddressTable (Address+)>
<!ELEMENT Address (Name, Street, City)>
<!ELEMENT Name (#PCDATA)>
<!ELEMENT Street (#PCDATA)>
<!ELEMENT City (#PCDATA)>
```

The rules of this DTD are:

- Text is allowed only in elements declared with `#PCDATA`.
- The document has a single document headline and a single address table.
- The address table has one or more addresses.
- Each address has the three sub-elements *Name*, *Street* and *City*.

With these rules, elements can be inserted or removed only as complete addresses. Inserting or removing any of the other elements results in an invalid document, and an [XMLInvalid](https://docs.textcontrol.com/tx-text-control-activex/reference/txtextcontrol/xmlinvalid-event/llms-full.txt) event occurs when a program tries it.

### Insert an Element

To insert an address, create a new [XMLElement](https://docs.textcontrol.com/tx-text-control-activex/reference/xmlelement/llms-full.txt) object, initialize it with text and call the [Add](https://docs.textcontrol.com/tx-text-control-activex/reference/xmlelements/add-method/llms-full.txt) method of the *Address* collection:

```vbscript
Dim NewAddress As Object
Set NewAddress = CreateObject("TIS.TX.XMLElement.34")
NewAddress.Text = "Paul Baskerville" & Chr(10) & "Jackson Street" & Chr(10) & "London"
TXTextControl1.XMLElements("Address").Add NewAddress, , 2
```

The code works as follows:

- `CreateObject` creates the new `XMLElement` object from its class name, `TIS.TX.XMLElement.34`.
- The [Text](https://docs.textcontrol.com/tx-text-control-activex/reference/xmlelement/text-property/llms-full.txt) property fills the name, street and city of the new address, separated by `Chr(10)` characters.
- [Add](https://docs.textcontrol.com/tx-text-control-activex/reference/xmlelements/add-method/llms-full.txt) inserts the new address after the second address in the document. With its *before* parameter, an element is inserted before a certain element instead.

### Delete an Element

The following line of code removes the address inserted above:

```vbscript
TXTextControl1.XMLElements("Address").Remove(3)
```

[Remove](https://docs.textcontrol.com/tx-text-control-activex/reference/xmlelements/remove-method/llms-full.txt) is also checked against the DTD. Because the DTD requires at least one address, trying to remove the last address results in an invalid document, and an [XMLInvalid](https://docs.textcontrol.com/tx-text-control-activex/reference/txtextcontrol/xmlinvalid-event/llms-full.txt) event occurs.

### Key Members

| Member | Description |
|---|---|
| [TXTextControl.XMLElements](https://docs.textcontrol.com/tx-text-control-activex/reference/txtextcontrol/xmlelements-property/llms-full.txt) | Returns the collection of all XML elements with the same tag name. |
| [XMLElements](https://docs.textcontrol.com/tx-text-control-activex/reference/xmlelements/llms-full.txt) | The collection of the elements of one kind. |
| [XMLElements.Item](https://docs.textcontrol.com/tx-text-control-activex/reference/xmlelements/item-method/llms-full.txt) | Returns a single element; the default method of the collection. |
| [XMLElements.Count](https://docs.textcontrol.com/tx-text-control-activex/reference/xmlelements/count-property/llms-full.txt) | The number of elements in the collection. |
| [XMLElements.Add](https://docs.textcontrol.com/tx-text-control-activex/reference/xmlelements/add-method/llms-full.txt) | Inserts an element before or after another element. |
| [XMLElements.Remove](https://docs.textcontrol.com/tx-text-control-activex/reference/xmlelements/remove-method/llms-full.txt) | Deletes an element. |
| [XMLElement](https://docs.textcontrol.com/tx-text-control-activex/reference/xmlelement/llms-full.txt) | One element of an XML document. |
| [XMLElement.Text](https://docs.textcontrol.com/tx-text-control-activex/reference/xmlelement/text-property/llms-full.txt) | Returns or sets the text of an element and its sub-elements. |
| [TXTextControl.XMLInvalid](https://docs.textcontrol.com/tx-text-control-activex/reference/txtextcontrol/xmlinvalid-event/llms-full.txt) | Occurs when a change would make the document invalid. |

### Next Steps

- [Editing XML Files](https://docs.textcontrol.com/tx-text-control-activex/document-editor/xml/editing/llms-full.txt): what users can do in the XML editor mode.
- [Loading and Saving XML Files](https://docs.textcontrol.com/tx-text-control-activex/document-editor/xml/load-save/llms-full.txt): validation, load errors and saving the CSS file.
- [XML Basics](https://docs.textcontrol.com/tx-text-control-activex/document-editor/xml/basics/llms-full.txt): the rules of a DTD in the XML Basics sample program.

---

## Nearby

- **Previous:** [Editing XML Files](https://docs.textcontrol.com/tx-text-control-activex/document-editor/xml/editing/llms-full.txt)
- **Next:** [XML Basics](https://docs.textcontrol.com/tx-text-control-activex/document-editor/xml/basics/llms-full.txt)
