獲取html標簽屬性


為了獲取html代碼一些標簽屬性,我們可以把這些html當作為一段xml的字符串來處理。可以參考下圖:

 


上圖相關代碼:

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);
    }
}

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM