如何从HTML模板中删除注释代码 [英] How to remove commented code from HTML template
问题描述
大家好,
从c#代码中删除html模板中注释代码的最佳方法
Hi All,
what is best way to remove commented code from html template in c# code behind
<html>
<head>
<title>Capital League</title>
</head>
<body style="color:#000000;">
<center>
<table align="center" border="0" cellpadding="0" cellspacing="0" height="100%" width="100%"
id="bodyTable">
<tbody>
<tr>
<td align="center" valign="top" id="bodyCell" class="transfer_panel_main" style="font-family: verdana, sans-serif;">
<!-- BEGIN TEMPLATE // -->
<!--[if gte mso 9]>
<table align="center" border="0" cellspacing="0" cellpadding="0" width="600" style="width:600px;">
<tr>
<td align="center" valign="top" width="600" style="width:600px;">
<![endif]-->
<table cellpadding="0" cellspacing="0" class="templateContainer" style="width: 600px;
table-layout: fixed; border: 1px solid #ccc;">
<tbody>
<tr>
<td class="header_background props_bar" valign="top" align="center" style="width: 100%;
float: left; background-color: #fff;">
<!--[if gte mso 9] |(IE)]>
<table align="center" border="0" cellspacing="0" cellPadding="0" width="100%" style="width:100%;"><![endif]-->
<table align="left" border="0" cellpadding="0" cellspacing="0" width="100%" class="columnWrapper">
<tbody>
<tr>
<td valign="top" class="mcnTextBlockInner" style="background-color:#ffffff;">
<!--[if gte mso 9] |(IE)]>
<table align="center" border="0" cellspacing="0" cellPadding="0" width="100%" style="width:100%;"><tr>
<td align="left" valign="top" width="100%" style="width:100%;"><![endif]-->
<table align="left" border="0" cellpadding="0" cellspacing="0" width="100%" class="columnWrapper displayOff">
<tbody>
<tr>
-- and so on
</body>
</html>
我尝试过:
var filepath = HttpContext.Current.Server.MapPath(〜/ Assets / TransactionEmailFiles / cd.html);
string htmlContent = File.ReadAllText(filepath );
var html = htmlContent;
string pattern1 = @<! - 。* - >;
string input = html;
string replacement =;
Regex rgx = new Regex(pattern1);
string result1 = rgx.Replace(输入,替换);
What I have tried:
var filepath=HttpContext.Current.Server.MapPath("~/Assets/TransactionEmailFiles/cd.html");
string htmlContent = File.ReadAllText(filepath);
var html = htmlContent ;
string pattern1 = @"<!--.*-->";
string input = html;
string replacement = " ";
Regex rgx = new Regex(pattern1);
string result1 = rgx.Replace(input, replacement);
推荐答案
缺少正确的正则表达式模式:
You lack the correct regex pattern:
<!--(.|\n)*-->
请参阅演示:
see demo:
using System;
using System.Text.RegularExpressions;
public class Program
{
public static void Main()
{
string pattern1 = @"<!--(.|\n)*-->";
string input = @"<td valign=""top"" class=""mcnTextBlockInner"" style=""background-color:#ffffff;"">
<!--[if gte mso 9] |(IE)]>
<table align=""center"" border=""0"" cellspacing=""0"" cellPadding=""0"" width=""100%"" style=""width:100%;""><tr>
<td align=""left"" valign=""top"" width=""00%"" style=""width:100%;""><![endif]-->
<table align=""left"" border=""0"" cellpadding=""0"" cellspacing=""0"" width=""100%"" class=""columnWrapper displayOff"">
<tbody>
<tr>";
string replacement = " ";
Regex rgx = new Regex(pattern1);
string result1 = rgx.Replace(input, replacement);
Console.WriteLine(result1);
}
}
输出减去评论:
<td valign="top" class="mcnTextBlockInner" style="background-color:#ffffff;">
<table align="left" border="0" cellpadding="0" cellspacing="0" width="100%" class="columnWrapper displayOff">
<tbody>
<tr>
这篇关于如何从HTML模板中删除注释代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!