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>";

 

posted @ 2015-12-16 17:32  哈佛  阅读(2632)  评论(0编辑  收藏  举报