C#正则表达式去除XML标签


案例1:

//数据源

String strSource = "<Sample>xxx<Extract>100</Extract></Sample> 11 <Extract>100<Extract>";

 

//表达式

String matchpattern = @"<([^>]*)>(.*?)<\/\1>";

//$2=(.*?) 进行替换

String replacementpattern = @"$2";

//循环判断 是否还有正确的XML标签
while (Regex.IsMatch(strSource, matchpattern))
{
strSource = Regex.Replace(strSource, matchpattern, replacementpattern, RegexOptions.IgnoreCase);
}

//输出结果:

//xxx100 11 <Extract>100<Extract>

案例2:

//标签中带属性

String strSource = "<Sample Name='Sample '>xxx<Extract>100</Extract></Sample> 11 <Extract>100<Extract>";

//表达式

String matchpattern = @"<(\w+)([^>]*)>(.*?)<\/\1>";

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM