I am trying to work on a regular expression to replace XML content in C# with no success.
Here is the sample code with XML example.
static void Main(string[] args)
{
Console.Write(ReplaceValue("<test val='123'><this>something</this></test>", "ANY_XML_BLOB", @"<test[^>]*>\s*(?'value'[^</test]*)"));
}
static string ReplaceValue(string request, string newFieldValue, string pat)
{
string value = String.Empty;
Regex conditionRex = new Regex(pat, RegexOptions.IgnoreCase | RegexOptions.Compiled);
Match match = conditionRex.Match(request);
if(match.Success)
{
value = match.Groups["value"].Value;
return request.Replace(value, newFieldValue);
}
return request;
}
}
Expected output is "this" tag and all sub-contents be replaced by the word ANY_XML_BLOB.
Any help fixing this would be appreciated.
Thanks!