日期:2014-05-17  浏览次数:20585 次

大家帮忙看下解析自定义标签问题。
标签形式:
HTML code
<div>
  <ul>
    <!--{$news num="10" classid="1" bytype="0"}-->
    <li><a href="{news.url}" target="_blank"{news.style}>·{news.i}、{news.title}</a>{news.AddTime dateformat="mm-dd"}</li>
    <!--{/$news}-->
  </ul>
</div>


现在可以解析,但不能解析:{news.AddTime dateformat="mm-dd"},也就是字段的属性
这是我写的代码:
C# code
        public string NewsList(string content)
        {
            string Attributes = "";
            string Text = "";
            string AllText = "";
            Regex r = new Regex(@"(\{\$news\s+(?<attributes>[^\]]*?)\}(?<text>[\s\S]*?)\{/\$news\})", RegexOptions.Compiled | RegexOptions.IgnoreCase);
            foreach (Match m in r.Matches(content))
            {
                Attributes = m.Groups["attributes"].ToString();
                Text = m.Groups["text"].ToString();
                AllText = m.Groups[0].Value.ToString();
                string TopNum = null;
                TopNum = GetAttr(Attributes, "num");
                if (TopNum != null)
                {
                    TopNum = " Top " + TopNum + " ";
                }
                string strSQL = "Select " + TopNum + " * From HL_News Where 1 = 1 ";
                string ClassId = null;
                ClassId = GetAttr(Attributes, "classid");
                if (ClassId != null)
                {
                    strSQL += " And ClassId = " + ClassId + " ";
                }
                strSQL += " Order By id Desc ";
                SqlConnection con = dbConn.GetConn();
                con.Open();
                SqlDataAdapter da = new SqlDataAdapter(strSQL, con);
                DataSet ds = new DataSet();
                da.Fill(ds);

                if (ds.Tables[0].Rows.Count > 0)
                {
                    int index = 1;
                    StringBuilder str = new StringBuilder();
                    foreach (DataRow dr in ds.Tables[0].Rows)
                    {
                        StringBuilder txt = new StringBuilder();
                        txt.Append(Text);
                        txt = txt.Replace("{news.classid}", dr["classid"].ToString());
                        txt = txt.Replace("{news.title}", dr["title"].ToString());
                        txt = txt.Replace("{news.url}", dr["id"].ToString());
                        txt = txt.Replace("{news.i}", index.ToString());
                        index++;
                        str.Append(txt);
                    }
                    content = content.Replace(AllText, str.ToString());
                }
            }
            return content;
        }
        /// <summary>
        /// 获取属性的值
        /// </summary>
        /// <param name="tag">str</param>
        /// <param name="tagName">属性名称</param>
        /// <returns>属性的值</returns>
        private string GetAttr(string tag, string tagName)
        {
            Regex r = new Regex(@"(?<key>\w+)\s*=\s*(?<value>" + "\"" + "[^\"]*\")", RegexOptions.Compiled | RegexOptions.IgnoreCase);
            foreach (Match m in r.Matches(tag))
            {
                if (m.Groups["key"].ToString().Trim().ToLower() == tagName.ToLower())
                {
                    return m.Groups["