chatura
chatura

Reputation: 4117

C# using regular expressions to replace malformed XML close tags

I have a malformed xml file which is generated with incorrect closing tags as follows.

<Root>
.
.
<Question id='1' type='text'>London</Question id='1' type='text'>
<Question id='2' type='radio'>4</Question id='2' type='radio'>
<Question id='3' type='check'>6</Question id='3' type='check'>
.
.
</Root>

I need to refine this XML file with propper closing tags as follows.

<Question id='1' type='text'>London</Question>

In summary close tags like,

<Question id='some id' type='some type'> should be replaced with </Question>

There are hundreds of tags in the file. How can I use string operations with RegEx to process that file in order to create a well-formed XML file.

Thanks,

Chatur

Upvotes: 0

Views: 982

Answers (1)

Artem Koshelev
Artem Koshelev

Reputation: 10607

Assuming str is the malformed XML string:

string fixed = Regex.Replace(str, @"</([^\s]+)[^>]+>", "</$1>");

A very useful thing to test regular expressions is Regex Designer from Rad Software. It's free, it's fully .NET-compatible and it has built-in help.

Upvotes: 2

Related Questions