It isn't a good idea to parse XML with regexes unless you understand your data. I would argue that in some limited cases it can be very helpful. @HighCore, see this answer to the same question.
We're not trying to understand all possible input in the world—we're trying to make something that works in a specific case. So, if you know that your input doesn't have <
or >
in the data, only in the node names, you can use a regex.
In C#, use a MatchEvaluator
like so:
class MyReplacer {
public string ReplaceSpaces(Match m)
{
return m.Value.Replace(" ", "_");
}
void replacingMethod() {
...
Regex re = new Regex("<.*>");
MyReplacer r = new MyReplacer();
// Assign the replace method to the MatchEvaluator delegate.
MatchEvaluator myEvaluator = new MatchEvaluator(r.ReplaceSpaces);
// Replace matched characters using the delegate method.
sInput = re.Replace(sInput, myEvaluator);
}