继续浏览精彩内容
慕课网APP
程序员的梦工厂
打开
继续
感谢您的支持,我会继续努力的
赞赏金额会直接到老师账户
将二维码发送给自己后长按识别
微信支付
支付宝支付

获取html标签属性

慕村225694
关注TA
已关注
手记 95
粉丝 7
获赞 27

为了获取html代码一些标签属性,我们可以把这些html当作为一段xml的字符串来处理。可以参考下图:https://img3.mukewang.com/5addaad20001523c10440502.jpg

 


上图相关代码:

View Code using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Xml;

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        string htmlcode = "<img alt=\"mvp logo\" src=\"http://images.cnblogs.com/cnblogs_com/insus/MvpPhoto.jpg\" width=\"120\" height=\"30\" />";

        XmlNodeList xnl = GetNoteList(htmlcode, "//img");
        for (int i = 0; i < xnl.Count; i++)
        {
            Response.Write(xnl[i].Attributes["alt"].Value + "<br/>");
            Response.Write(xnl[i].Attributes["src"].Value + "<br/>");
            Response.Write(xnl[i].Attributes["width"].Value + "<br/>");
            Response.Write(xnl[i].Attributes["height"].Value + "<br/>");
        }
    }

    private XmlNodeList GetNoteList(string xmlText, string tag)
    {
        XmlDocument xDoc = new XmlDocument();
        xDoc.Load(new System.IO.MemoryStream(System.Text.Encoding.GetEncoding("UTF-8").GetBytes(xmlText)));
        return xDoc.SelectNodes(tag);
    }
}

 

 

打开App,阅读手记
0人推荐
发表评论
随时随地看视频慕课网APP