Regular expression to remove special characters in XML string using C# in ASP.Net

YulIhnio
 
on Dec 17, 2020 09:06 AM
3664 Views

I am trying to remove the special characters of a string that will be included in a service but as I am using it removes certain tags, I have tried with a regular expression but I only need to validate the content that is inside the tags <> "here" </>

Regex estructureXml = new Regex(@"(?<initialTag>\<.+?\>)(?<content>.+?)(?<finalTag>\</.+?\>)", RegexOptions.Compiled);
Regex filter = new Regex(@"[^\w]", RegexOptions.Compiled);
string xmlClean = estructureXml.Replace(xmlTest, new MatchEvaluator(c =>
		  {
		       return $"{c.Groups["initialTag"].Value}{filter.Replace(c.Groups["content"].Value, string.Empty)}{c.Groups["finalTag"].Value}";
		  }));

this is the xml that I test in the string 

<case-data process="123456"> 
<description>sadáwwwwwlsdasdas,.-#$%$&%& hoila fhd!°$%&</description> 
<accounts>123-usser</accounts> 
<parameters> 
<parameter_process id="1298">123456,.-()/*--+!$%78</parameter_process> 
<parameter_process id="1299">mail-//(&&(/@mail.com</parameter_process> 
</parameters> 
</case-data>

I also try with this but i thinks it is not the best way 

strXml = Regex.Replace(strXml, @"([^</\w\s-="">@.])", "") ;

 

Download FREE API for Word, Excel and PDF in ASP.Net: Download
dharmendr
 
on Dec 18, 2020 04:05 AM

Use below code.

Regex estructureXml = new Regex(@"(?<initialTag>\<.+?\>)(?<content>.+?)(?<finalTag>\</.+?\>)", RegexOptions.Compiled);
Regex filter = new Regex(@"[^</\w\s="">@.]", RegexOptions.Compiled);
string xmlClean = estructureXml.Replace(xmlTest, new MatchEvaluator(c =>
{
    return string.Format("{0}{1}{2}",
        c.Groups["initialTag"].Value,
        filter.Replace(c.Groups["content"].Value, string.Empty),
        c.Groups["finalTag"].Value);
}));