# XmlElement.AddChild Method

> Adds a new XML element as a sub-element of this element. The new element is placed at the position specified through the document's DTD.

- **Product:** TX Text Control .NET for Windows Forms
- **Section:** Reference
- **Kind:** Method
- **Namespace:** `TXTextControl`
- **Package:** `TXTextControl.TextControl.WinForms.SDK` (assembly `TXTextControl.dll`, from the private NuGet feed or Text Control Offline Packages)
- **.NET Framework:** reference `TXTextControl.dll` from the setup
- **Path:** TX Text Control .NET for Windows Forms > Reference > TXTextControl > XmlElement.AddChild Method
- **1154 words**
- **Web URL:** https://docs.textcontrol.com/tx-text-control-dotnet-winforms/reference/txtextcontrol/xmlelement/addchild-method/
- **LLMs URL:** https://docs.textcontrol.com/tx-text-control-dotnet-winforms/reference/txtextcontrol/xmlelement/addchild-method/llms.txt
- **LLMs-Full URL:** https://docs.textcontrol.com/tx-text-control-dotnet-winforms/reference/txtextcontrol/xmlelement/addchild-method/llms-full.txt

---

### Description

Adds a new XML element as a sub-element of this element. The new element is placed at the position specified through the document's DTD.

### Syntax

#### AddChild(XmlElement)

Adds the specified XML element as a sub-element of this element. The new element is placed at the position specified through the document's DTD.

```csharp
public bool AddChild(XmlElement element);
```

```vbnet
Public Function AddChild(element As XmlElement) As Boolean
```

| Parameter | Description |
|---|---|
| element | Specifies the XML element to add. |

#### AddChild(XmlElement, int, int)

Adds the specified XML element 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.

```csharp
public bool AddChild(XmlElement element, int before, int after);
```

```vbnet
Public Function AddChild(element As XmlElement, before As Integer, after As Integer) As Boolean
```

| Parameter | Description |
|---|---|
| element | Specifies the XML element to add. |
| before | Specifies a relative position in the list of child elements of the specified type beginning with 1. 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. If this parameter is -1, it is ignored. |
| after | Specifies a relative position in the list of child elements of the specified type beginning with 1. 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. If this parameter is -1, it is ignored. |

### Return Value

The return is false, if the new element could not be added because the document becomes invalid according to the document's DTD. In this case an XmlInvalid event occurs. The return value is true, if the new element could successfully been added.

### Examples

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

```csharp
TXTextControl.XmlElement newElement = new TXTextControl.XmlElement("Telephone", "046798 / 3459786");
textControl1.GetXmlElements("Adress").GetItem(2).AddChild(newElement);
```

```vbnet
Dim NewElement As New TXTextControl.XmlElement("Telephone", "046798 / 3459786")
TextControl1.GetXmlElements("Adress").GetItem(2).AddChild(NewElement)
```

The following example adds a child XML element.

```csharp
if (xmlElement.AddChild(new XmlElement("CustomerName")))
{
    Console.WriteLine("Add Child completed successfully.");
}
```

```vbnet
If xmlElement.AddChild(New XmlElement("CustomerName")) Then
    Console.WriteLine("Add Child completed successfully.")
End If
```

---

## Nearby

- **Previous:** [XmlElement Class](https://docs.textcontrol.com/tx-text-control-dotnet-winforms/reference/txtextcontrol/xmlelement/llms-full.txt)
- **Next:** [XmlElement.AutoSelect Property](https://docs.textcontrol.com/tx-text-control-dotnet-winforms/reference/txtextcontrol/xmlelement/autoselect-property/llms-full.txt)
