# AddChild Method

> Adds an XMLElement as a sub-element at the position the DTD gives, optionally before or after a child; returns 0 on error, 1 if invalid, 2 on success.

- **Product:** TX Text Control ActiveX
- **Section:** Documentation
- **Kind:** Method
- **Path:** TX Text Control ActiveX > API Reference > XMLElement > AddChild Method
- **559 words**
- **Web URL:** https://docs.textcontrol.com/tx-text-control-activex/reference/xmlelement/addchild-method/
- **LLMs URL:** https://docs.textcontrol.com/tx-text-control-activex/reference/xmlelement/addchild-method/llms.txt
- **LLMs-Full URL:** https://docs.textcontrol.com/tx-text-control-activex/reference/xmlelement/addchild-method/llms-full.txt

---

Adds a new [XMLElement](https://docs.textcontrol.com/tx-text-control-activex/reference/xmlelement/llms-full.txt) object as a sub-element of this element. The new element is placed at the position specified through the document's DTD. If this element can have more than one child of the specified type the before or after parameters can be used to specify a position.

### Syntax

```vbscript
XMLElement.AddChild 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 list of child elements of the specified type. The XML element to be added is placed before the child identified by the before parameter. You can specify before or after positions but not both. |
| after | Optional. Specifies a relative position in the list of child elements of the specified type. The XML element to be added is placed after the child identified by the after parameter. You can specify before or after positions but not both. |

### Return Value

The method returns one of the following values:

| 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 been successfully added. |

### Examples

The following Basic example creates a new "Telephone" element and adds it to the second address in an XML document that contains addresses consisting of the sub-elements Name, Street, City and Telephone:

```vbscript
Dim NewElement As Object
Set NewElement = CreateObject("TIS.TX.XMLElement.34")
NewElement.ElementName = "Telephone"
NewElement.Text = "034568-782346"
TXTextControl1.XMLElements("Address")(2).AddChild NewElement
```

### 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)
- [XMLElement.RemoveChild Method](https://docs.textcontrol.com/tx-text-control-activex/reference/xmlelement/removechild-method/llms-full.txt)
- [Programming Category: XML Programming](https://docs.textcontrol.com/tx-text-control-activex/reference/categories/xml/llms-full.txt)

---

## Nearby

- **Previous:** [XMLElement Object](https://docs.textcontrol.com/tx-text-control-activex/reference/xmlelement/llms-full.txt)
- **Next:** [AutoSelect Property](https://docs.textcontrol.com/tx-text-control-activex/reference/xmlelement/autoselect-property/llms-full.txt)
