# Add Method

> Adds an XMLElement to an XMLElements collection, optionally before or after a member; returns 0 on error, 1 if the DTD makes it invalid, 2 on success.

- **Product:** TX Text Control ActiveX
- **Section:** Documentation
- **Kind:** Method
- **Path:** TX Text Control ActiveX > API Reference > XMLElements > Add Method
- **693 words**
- **Web URL:** https://docs.textcontrol.com/tx-text-control-activex/reference/xmlelements/add-method/
- **LLMs URL:** https://docs.textcontrol.com/tx-text-control-activex/reference/xmlelements/add-method/llms.txt
- **LLMs-Full URL:** https://docs.textcontrol.com/tx-text-control-activex/reference/xmlelements/add-method/llms-full.txt

---

Adds a new [XMLElement](https://docs.textcontrol.com/tx-text-control-activex/reference/xmlelement/llms-full.txt) object to the collection.

### Syntax

```vbscript
XMLElements.Add XMLElement[, before][, after]
```

### Parameters

| Parameter | Description |
|---|---|
| XMLElement | Specifies an [XMLElement](https://docs.textcontrol.com/tx-text-control-activex/reference/xmlelement/llms-full.txt) object. |
| before | Optional. Specifies a relative position in the collection. The XML element to be added is placed in the collection before the member identified by the before parameter. It must be a number from 1 to the value of the collection's [Count](https://docs.textcontrol.com/tx-text-control-activex/reference/xmlelements/count-property/llms-full.txt) property. If this parameter is zero, the new element is inserted before the element at the current input position. You can specify before or after positions but not both. |
| after | Optional. Specifies a relative position in the collection. The XML element to be added is placed in the collection after the member identified by the after parameter. It must be a number from 1 to the value of the collection's [Count](https://docs.textcontrol.com/tx-text-control-activex/reference/xmlelements/count-property/llms-full.txt) property. If this parameter is zero the new element is inserted after the element at the current input position. You can specify before or after positions but not both. |

### Return Value

| Value | Description |
|---|---|
| 0 | An error has occurred. |
| 1 | The new element could not be added because the document becomes invalid according to the document's DTD. In this case Text Control fires an [XMLInvalid](https://docs.textcontrol.com/tx-text-control-activex/reference/txtextcontrol/xmlinvalid-event/llms-full.txt) event. |
| 2 | The new element has successfully been added. |

### Data Types

| Name | Data type |
|---|---|
| XMLElement | [XMLElement Object](https://docs.textcontrol.com/tx-text-control-activex/reference/xmlelement/llms-full.txt) |
| before | Long |
| after | Long |
| Return value | Integer |

### Examples

The following Basic example inserts a new Address element in an XML document that contains addresses consisting of the sub-elements Name, Street and City. The new address is inserted after the second address:

```vbscript
Dim NewAddress As Object
Set NewAddress = CreateObject("TIS.TX.XMLElement.34")
NewAddress.Text = "Jack Brown" & Chr(10) _
& "Baker Street" & Chr(10) & "New York"
TXTextControl1.XMLElements("Address").Add NewAddress, , 2
```

### See Also

- [Working with XML Files](https://docs.textcontrol.com/tx-text-control-activex/document-editor/xml/llms-full.txt)
- [XMLElement Object](https://docs.textcontrol.com/tx-text-control-activex/reference/xmlelement/llms-full.txt)
- [XMLElements Collection](https://docs.textcontrol.com/tx-text-control-activex/reference/xmlelements/llms-full.txt)
- [Programming Category: XML Programming](https://docs.textcontrol.com/tx-text-control-activex/reference/categories/xml/llms-full.txt)

---

## Nearby

- **Previous:** [XMLElements Collection Object](https://docs.textcontrol.com/tx-text-control-activex/reference/xmlelements/llms-full.txt)
- **Next:** [Count Property](https://docs.textcontrol.com/tx-text-control-activex/reference/xmlelements/count-property/llms-full.txt)
