Miguel
Miguel

Reputation: 558

Convert XML self closing tags to open and closing tag

I have an string that is a full XML documento where i should replace self closing tags to opening and closing tag.

For example: <name />

Should be converted to: <name></name>

I tried without regex with:

myXML.Replace("<name />","<name></name>");

Works but only for this tag, that is the resaon why i would like to do it with regex, to support all possible XML tags of the XML Document without doing a replace of a particular XML node.

I accept any suggestion to solve the problem. Thanks in advance.

Upvotes: 1

Views: 5851

Answers (2)

casperengl
casperengl

Reputation: 1

The accepted answer will put potential attributes on the closing tag aswell as the opening tag.

I had to create closing tags for a collection of SVGs to support IE11. Use the following solution, if you need to have attributes only on the opening tag.

Find: <([a-z]+)+(.*?)\s*/>
Replace: <$1$2></$1>

This solution creates two different regex groups. $1 contains the XML tag. $2 contains the attributes.

Upvotes: 0

Jan
Jan

Reputation: 43169

As an answer as well: You may use

<(.*?)\s*/> 

and replace this with <$1></$1>, see a demo on regex101.com.

Upvotes: 4

Related Questions