网站开发技术手记-读取html标签内容

1。读取单标签内容:

    private string GetSingleTagValueByAttr(string inputstring,string tagName,string attrname,string key)
    {
        Regex reg = new Regex("<" + tagName + " [^<>]*>",RegexOptions.IgnoreCase);
        MatchCollection matchs = reg.Matches(inputstring);
        string result = "";
        foreach (Match match in matchs)
        {
            string matchValue = match.Value;
            Regex regValue = new Regex("content=/".*/"",RegexOptions.IgnoreCase);
            if (matchValue.ToLower().IndexOf(attrname + "=/"" + key + "/"") != -1)
            {
                if(regValue.IsMatch(matchValue))
                {
                    result = regValue.Match(matchValue).Value;
                    if (!string.IsNullOrEmpty(result))
                    {
                        result =result.ToLower().Replace("content=","").Replace("/"","");
                    }
                }
                return result;
            }
        }
        return "";
    }
使用举例:
string description=GetSingleTagValueByAttr(sourceHTML,“meta”,"name","description");
string keywords =GetSingleTagValueByAttr(sourceHTML,“meta”,"name","keywords");
改进:添加读取属性参数,目前只限定了读取的是meta的congtent的内容。
2。读取封闭标签的内容:
    private string GetFixTagContent(string inputstring,string tagName)
    {
        string leftTag = "<" + tagName + ">";
        string rightTag="</"+tagName+">";
        
        Regex reg = new Regex("<" +tagName+"[^<>]*>(.|/n)*" + rightTag,RegexOptions.IgnoreCase);
        return reg.Match(inputstring).Value.Replace(leftTag, "").Replace(rightTag, "");
    }
使用举例:
string title=GetFixTagContent(sourceHTML,"title");

============
09/22/2007 更新:
    private string GetSingleTagValueByAttr(string inputstring,string tagName,string attrname,string key)
    {
        Regex reg = new Regex("<" + tagName + " [^<>]*>",RegexOptions.IgnoreCase);
        MatchCollection matchs = reg.Matches(inputstring);
        string result = "";
        foreach (Match match in matchs)
        {
            string matchValue = match.Value;
            Regex regValue = new Regex("content=/"[^<>/"]*/"",RegexOptions.IgnoreCase);
            if (matchValue.ToLower().IndexOf(attrname + "=/"" + key + "/"") != -1)
            {
                if(regValue.IsMatch(matchValue))
                {
                    result = regValue.Match(matchValue).Value;
                    if (!string.IsNullOrEmpty(result))
                    {
                        result =result.ToLower().Replace("content=","").Replace("/"","");
                    }
                }
                return result;
            }
        }
        return "";
    }
    private string GetFixTagContent(string inputstring,string tagName)
    {
        string regleftTag = "<" + tagName + "[^<>]*>";
        string rightTag="</"+tagName+">";

        Regex reg = new Regex(regleftTag + "(.|/n)*" + rightTag, RegexOptions.IgnoreCase);
        string result = reg.Match(inputstring.ToLower()).Value;
        if (string.IsNullOrEmpty(result)) return "";
        return Server.HtmlDecode(result.Replace(new Regex(regleftTag, RegexOptions.IgnoreCase).Match(result).ToString(), "").Replace(rightTag, ""));
    }

来自:http://www.cnblogs.com/neilvension/archive/2007/09/13/993419.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值