# Search and Replace with the XmlDocument

Understand how to implement the search and replace functionality using the XmlDocument object model in C1Editor for WinForms control.

## Content



To perform the same search/replace task with the **XmlDocument** object model, you would use the following code:

DOC-DETAILS-TAG-OPEN

DOC-SUMMARY-TAG-OPEN

To write code in C#

DOC-SUMMARY-TAG-CLOSE

```csharp
void replaceUsingXmlDocument(object sender, EventArgs e)
{
    XmlDocument doc = this.c1Editor1.Document;
    XmlNodeList nodes = SelectNodes(doc, "/html/body");
    if (nodes.Count > 0)
    {
        ReplaceNodeText(nodes[0], "user", "customer");
    }
}
```

DOC-DETAILS-TAG-CLOSE

The code uses the **SelectNodes** method described earlier to select a single node that represents the document's body. It then calls the helper method **ReplaceNodeText** given below:

DOC-DETAILS-TAG-OPEN

DOC-SUMMARY-TAG-OPEN

To write code in C#

DOC-SUMMARY-TAG-CLOSE

```csharp
void ReplaceNodeText(XmlNode node, string search, string replace)
{
    foreach (XmlNode child in node.ChildNodes)
    {
        ReplaceNodeText(child, search, replace);
    }
    if (node.NodeType == XmlNodeType.Text)
    {
        node.InnerText = node.InnerText.Replace(search, replace);
    }
}
```

DOC-DETAILS-TAG-CLOSE

**ReplaceNodeText** then takes an **XmlNode** as a parameter and scans all the node's children by calling itself recursively until it reaches a text node. At that point, it uses the regular **string.Replace** method to modify the content of the node.

The changes made to the **XmlDocument** are automatically reflected in the [C1Editor](/componentone/api/win/online-richtexteditor/dotnet-framework-api/C1.Win.C1Editor.4.8/C1.Win.C1Editor.C1Editor.html). Exposing the power and flexibility of the **XmlDocument** object model for processing XHTML documents is one of the main advantages of the C1Editor control.