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