# XmlElementCollection.Add Method

> Adds the specified XML element to the collection.

- **Product:** TX Text Control .NET for WPF
- **Section:** Reference
- **Kind:** Method
- **Namespace:** `TXTextControl`
- **Package:** `TXTextControl.TextControl.WPF.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 WPF > Reference > TXTextControl > XmlElementCollection.Add Method
- **1204 words**
- **Web URL:** https://docs.textcontrol.com/tx-text-control-dotnet-wpf/reference/txtextcontrol/xmlelementcollection/add-method/
- **LLMs URL:** https://docs.textcontrol.com/tx-text-control-dotnet-wpf/reference/txtextcontrol/xmlelementcollection/add-method/llms.txt
- **LLMs-Full URL:** https://docs.textcontrol.com/tx-text-control-dotnet-wpf/reference/txtextcontrol/xmlelementcollection/add-method/llms-full.txt

---

### Description

Adds the specified XML element to the collection.

### Syntax

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

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

### Parameters

| Parameter | Description |
|---|---|
| element | Specified an instance of the XmlElement object. |
| before | Specifies a relative position in the collection. The XmlElement to be added is placed in the collection before the member identified by the before parameter. If this parameter is zero, the new element is inserted before the element at the current input position. If this parameter is -1, it is ignored. |
| after | 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. If this parameter is zero, the new element is inserted after the element at the current input position. If this parameter is -1, it is ignored. |

### Return Value

The return value 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 inserts a new Address element into an XML document that contains adresses consisting of the sub-elements Name, Street and City. The new address is inserted after the second address:

```csharp
TXTextControl.XmlElement newElement = new TXTextControl.XmlElement("Telephone", "46798 / 3459786");
newElement.Text("Jack Brown\nBaker Street\nNew York");
textControl1.GetXmlElements("Adress").Add(newElement, -1, 2);
```

```vbnet
Dim NewElement As New TXTextControl.XmlElement("Telephone", "46798 / 3459786")
NewElement.Text("Jack Brown" & Chr(10) & "Baker Street" & Chr(10) & "New York")
TextControl1.GetXmlElements("Adress").Add(NewElement, -1, 2)
```

The following example marks a customer name in the document with a named XML element.

```csharp
textControl.Text = "Customer: Contoso Ltd.";
XmlElementCollection elements = textControl.GetXmlElements("CustomerName");
XmlElement customer = new("CustomerName");
if (!elements.Add(customer, 11, 12))
{
    throw new InvalidOperationException("The customer XML element could not be inserted.");
}
```

```vbnet
textControl.Text = "Customer: Contoso Ltd."
Dim elements As XmlElementCollection = textControl.GetXmlElements("CustomerName")
Dim customer As New XmlElement("CustomerName")
If Not elements.Add(customer, 11, 12) Then
    Throw New InvalidOperationException("The customer XML element could not be inserted.")
End If
```

---

## Nearby

- **Previous:** [XmlElementCollection Class](https://docs.textcontrol.com/tx-text-control-dotnet-wpf/reference/txtextcontrol/xmlelementcollection/llms-full.txt)
- **Next:** [XmlElementCollection.CopyTo Method](https://docs.textcontrol.com/tx-text-control-dotnet-wpf/reference/txtextcontrol/xmlelementcollection/copyto-method/llms-full.txt)
