C#截取範本HTML區段物件函式

  • 4051
  • 0
  • C#
  • 2009-11-22

摘要:C#截取範本HTML區段物件函式

最近為了寫報表,製作了報表範本讀入函式。
將HTML的報表範本讀入後,利用Regular Expression將各個區段讀出來,在自己想辦法Replace組裝一下。
目前是劣質版,也就是以下這個Expression尚有問題,萬一範本中的文字中有@就會掛掉啦!(不知有何高招可以解決並讀入

<!--#ABC_START#-->我要的區段<!--#ABC_END#-->

string RegexExpression = "<!--[#]" + TemplateName + "_START[#]-->(?<item>[^@]+)<!--[#]" + TemplateName + "_END[#]-->";
 

using System;
using System.Data;
using System.Configuration;
using System.IO;
using System.Text;
using System.Text.RegularExpressions;
using System.Collections.Generic;
/// <summary>
/// Summary description for ReportFunction
/// </summary>
public class ReportFunction
{
    //分頁語法
    public string PageBreak = "<div class='PageBreak'>&nbsp;</div>";

	public ReportFunction()
	{

	}
    //將參數清空(例"#NUMBER#" →"");
    public string GetParameterClear(string SourceString)
    {
        string TemplateString = "";
        int start_index;
        int end_index;
        string SubString="";

        start_index = SourceString.IndexOf('#');
        while (start_index >= 0)
        {
            end_index = SourceString.IndexOf('#', start_index + 1);
            SubString = SourceString.Substring(start_index, end_index - start_index + 1);
            SourceString = SourceString.Replace(SubString, "");
            start_index = SourceString.IndexOf('#');
        }
        return SourceString;
    }
    public string GetTempateString(string SourceString, string TemplateName)
    {
        string TemplateString = "";
        string RegexExpression = "<!--[#]" + TemplateName + "_START[#]-->(?<item>[^@]+)<!--[#]" + TemplateName + "_END[#]-->";
        Regex pattern = new Regex(RegexExpression, RegexOptions.IgnorePatternWhitespace);
        Match match = pattern.Match(SourceString);
        TemplateString = match.Value;
        TemplateString = TemplateString.Replace("<!--#" + TemplateName + "_START#-->", "");
        TemplateString = TemplateString.Replace("<!--#" + TemplateName + "_END#-->", "");
        return TemplateString;
    }
    public string GetTempateBeforeString(string SourceString, string TemplateName)
    {
        string TemplateString = "";
        string RegexExpression = "(?<item>[^@]+)<!--[#]" + TemplateName + "_START[#]-->";
        Regex pattern = new Regex(RegexExpression, RegexOptions.IgnorePatternWhitespace);
        Match match = pattern.Match(SourceString);
        TemplateString = match.Value;
        TemplateString = TemplateString.Replace("<!--#" + TemplateName + "_START#-->", "");
        return TemplateString;
    }
    public string GetTempateAfterString(string SourceString, string TemplateName)
    {
        string TemplateString = "";
        string RegexExpression = "<!--[#]" + TemplateName + "_END[#]-->(?<item>[^@]+)";
        Regex pattern = new Regex(RegexExpression, RegexOptions.IgnorePatternWhitespace);
        Match match = pattern.Match(SourceString);
        TemplateString = match.Value;
        TemplateString = TemplateString.Replace("<!--#" + TemplateName + "_END#-->", "");
        return TemplateString;
    }
    public string GetHtmlString(string HtmlPath)
    {
        string htmlString = "";
        try
        {
            //讀範本檔
            if (File.Exists(HtmlPath))
            {
                htmlString = File.ReadAllText(HtmlPath, Encoding.UTF8);
            }
            return htmlString;
        }
        catch (Exception ex)
        {
            throw new Exception("Export Report Template File Error");
        }
    }
    //將<!--#TAG_START#-->到<!--#TAG_END#-->區段全部刪除
    public string ReplaceTag(string SourceHtml, string ReplaceHtml, string TemplateName)
    {
        return SourceHtml.Replace("<!--#" + TemplateName.ToUpper() + "_START#-->" + ReplaceHtml + "<!--#" + TemplateName.ToUpper() + "_END#-->", "#" + TemplateName.ToUpper() + "_LIST#");
    }
}