为了账号安全,请及时绑定邮箱和手机立即绑定

获取html标签属性

标签:
C#

为了获取html代码一些标签属性,我们可以把这些html当作为一段xml的字符串来处理。可以参考下图:https://img1.sycdn.imooc.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);
    }
}

 

 

点击查看更多内容
TA 点赞

若觉得本文不错,就分享一下吧!

评论

作者其他优质文章

正在加载中
  • 推荐
  • 评论
  • 收藏
  • 共同学习,写下你的评论
感谢您的支持,我会继续努力的~
扫码打赏,你说多少就多少
赞赏金额会直接到老师账户
支付方式
打开微信扫一扫,即可进行扫码打赏哦
今天注册有机会得

100积分直接送

付费专栏免费学

大额优惠券免费领

立即参与 放弃机会
意见反馈 帮助中心 APP下载
官方微信

举报

0/150
提交
取消