Question: Is there a way to selectively replace XElement content with other XElements?

I have this XML:

```
<prompt>
   There is something I want to tell you.[pause=3]
   You are my favorite caller today.[pause=1]
   Have a great day!
</prompt>

```

And I want to render it as this:

```
<prompt>
   There is something I want to tell you.<break time="3s"/>
   You are my favorite caller today.<break time="1s"/>
   Have a great day!
</prompt>

```

I need to replace the placeholders with actual XElements, but when I try to alter the content of an XElement, .NET of course escapes all of the angle brackets. I understand why the content would normally need to be correctly escaped, but I need to bypass that behavior and inject XML directly into content.

Here's my code that would otherwise work.

```
MatchCollection matches = Regex.Matches(content, @"\[(\w+)=(\d+)]");
foreach (XElement element in voiceXmlDocument.Descendants("prompt"))
{
    if (matches[0] == null)
        continue;
    element.Value = element.Value.Replace(matches[0].Value, @"<break time=""5s""/>");
}

```

This is a work in progress, so don't worry so much about the validity of the RegEx pattern, as I will work that out later to match several conditions. This is proof of concept code and the focus is on replacing the placeholders as described. I only included the iteration and RegEx code here to illustrate that I need to be able to do this to a whole document that is already populated with content.

Answer: 
You can use `XElement.Parse()` method:

First, get the outer xml of your XElement, for example, 

```
string outerXml = element.ToString();

```

The you have exactly this string to work with:

```
<prompt>
  There is something I want to tell you.[pause=3]
  You are my favorite caller today.[pause=1]
  Have a great day!
</prompt>

```

Then you can do your replacement

```
outerXml = outerXml.Replace(matches[0].Value, @"<break time=""5s""/>");

```

Then you can parse it back:

```
XElement repElement = XElement.Parse(outerXml);

```

And, finally, replace original XElement:

```
element.ReplaceWith(repElement);

```
Oh, my goodness, you guys were quicker than I expected! So, thanks for that, however in the meantime, I solved it a slightly different way. The code here looks expanded from before because once I got it working, I added some specifics into this particular condition:

```
foreach (XElement element in voiceXmlDocument.Descendants("prompt").ToArray())
{
    // convert the element to a string and see to see if there are any instances
    // of pause placeholders in it
    string elementAsString = element.ToString();
    MatchCollection matches = Regex.Matches(elementAsString, @"\[pause=(\d+)]");
    if (matches == null || matches.Count == 0)
        continue;
    // if there were no matches or an empty set, move on to the next one

    // iterate through the indexed matches
    for (int i = 0; i < matches.Count; i++)
    {
        int pauseValue = 0; // capture the original pause value specified by the user
        int pauseMilliSeconds = 1000; // if things go wrong, use a 1 second default
        if (matches[i].Groups.Count == 2) // the value is expected to be in the second group
        {
            // if the value could be parsed to an integer, convert it from 1/8 seconds to milliseconds
            if (int.TryParse(matches[i].Groups[1].Value, out pauseValue))
                pauseMilliSeconds = pauseValue * 125; 
        }

        // replace the specific match with the new <break> tag content
        elementAsString = elementAsString.Replace(matches[i].Value, string.Format(@"<break time=""{0}ms""/>", pauseMilliSeconds));
    }

    // finally replace the element by parsing
    element.ReplaceWith(XElement.Parse(elementAsString));
}

```