2012年5月3日 星期四

Xpath 語法 - 使用 HtmlAgilityPack 於 C#

XPath即為XML路徑語言(XML Path Language),它是一種用來確定XML文檔中某部分位置的語言。

XPath基於XML的樹狀結構,提供在資料結構樹中找尋節點的能力。起初 XPath 的提出的初衷是將其作為一個通用的、介於XPointer與XSL間的語法模型。但是 XPath 很快的被開發者採用來當作小型查詢語言。

引用 & 參考:XPath 語言、XPath Axes、XML Path Language (XPath)

XPATH 基本語法

  • para selects the para element children of the context node

    para 選擇 para 子元素的本文節點


  • * selects all element children of the context node

    * 選擇所有子元素的本文節點


  • text() selects all text node children of the context node

    text() 選擇所有 text 子結點的本文節點


  • @name selects the name attribute of the context node

    @name 選擇所有 name 屬性的本文節點


  • @* selects all the attributes of the context node

    @* 選擇所有屬性的本文節點


  • para[1] selects the first para child of the context node

    para[1] 選擇所有第一個 para 元素的本文節點
  • para[last()] selects the last para child of the context node

    para[last()]選擇所最後一個 para 元素的本文節點


  • */para selects all para grandchildren of the context node

    */para 選擇所有 para 子孫的本文節點


  • /doc/chapter[5]/section[2] selects the second section of the fifth chapter of the doc

    /doc/chapter[5]/section[2] 選擇 doc 下的第五個 chapter 下的第二個 section 節點


  • chapter//para selects the para element descendants of the chapter element children of the context node

    chapter//para 選擇所有的父節點為chapter元素的para元素


  • //para selects all the para descendants of the document root and thus selects all para elements in the same document as the context node

    //para 選擇所有為 para 元素
  • //olist/item selects all the item elements in the same document as the context node that have an olist parent

    //olist/item 選擇所有父節點為 olist 元素的 item 元素


  • . selects the context node

    . 選擇當前節點


  • .//para selects the para element descendants of the context node

    .//para
  • 選擇當前節點的所有 para 子元素
  • .. selects the parent of the context node

    .. 選擇當前節點的父節點


  • ../@lang selects the lang attribute of the parent of the context node

    ../@lang 選擇名為 lang 的所有属性


  • para[@type="warning"] selects all para children of the context node that have a type attribute with value warning

    para[@type="warning"] 選擇所有 title 元素,且這些元素擁有值為 warning 的 lang 属性


  • para[@type="warning"][5] selects the fifth para child of the context node that has a type attribute with value warning

    para[@type="warning"][5] 選擇所有 title 元素,且這些元素擁有值為 warning 的 lang 属性的第五個節點


  • para[5][@type="warning"] selects the fifth para child of the context node if that child has a type attribute with value warning

    para[5][@type="warning"] 選擇所有 title 元素的第五個節點,且這個元素擁有值為 warning 的 lang 属性


  • chapter[title="Introduction"] selects the chapter children of the context node that have one or more title children with string-value equal to Introduction

    chapter[title="Introduction"] 選擇所有 chapter 元素,且其中的 title 元素的值等於 Introduction


  • chapter[title] selects the chapter children of the context node that have one or more title children

    chapter[title] 選擇所有 chapter 元素,且其中有 title 元素的值


  • employee[@secretary and @assistant] selects all the employee children of the context node that have both a secretary attribute and an assistant attribute


    employee[@secretary and @assistant] 選擇所有 employee 元素,且其中有 assistant 和 secretary 屬性的元素



XPATH 座標軸

  • ancestor 選擇當前節點的所有先輩(父、祖父等)
  • ancestor-or-self 選擇當前節点的所有先辈(父、祖父等)以及當前節點本身
  • attribute 選擇當前節點的所有屬性
  • child 選擇當前節點的所有子元素
  • descendant選擇當前節點的所有後代元素(子、孫等)
  • descendant-or-self 選擇當前節點的所有後代元素(子、孫等)以及當前節點本身
  • following 選擇文檔中當前節點的结束標籤之後的所有節點
  • namespace 選擇當前節點的所有命名空間節點
  • parent 選擇當前節點的父節點
  • preceding 選擇文檔中當前節點的開始標籤之前的所有節點
  • preceding-sibling 選擇當前節點之前的所有同级節點
  • self 選擇當前節點

使用範例

要擷取某些節點,必須要先觀察節點的獨特性質,或者是共通屬性,我通常都是使用 firefox 的外掛來觀察,IE 和 chrome 都有這種元件。
這次的例子我們在 google 上輸入 xpath,想要擷取前 10 筆網頁的標題,就先觀察它的網頁結構。

可以利用上面解說的方法,依照網頁結構來擷取,在程式中寫入以下 C# 程式碼:
HtmlWeb web;
HtmlDocument doc;
HtmlNodeCollection nodes;
string xp_title = string.Empty;


web = new HtmlWeb();
doc = web.Load("http://www.google.com/search?hl=en&q=xpath&oq=XPath");

xp_title = @"//h3[@class=""r""]";
nodes = doc.DocumentNode.SelectNodes(xp_title);

foreach (HtmlNode node in nodes)
{
    Console.WriteLine(node.InnerText);
}

執行結果如下:

比起 regular expression 輕鬆的是,它是以網頁結構去擷取,而 regular expression 是依照匹配字串做比對,所以在比對上 regular expression 比較有難度。
但是,在特殊情況下,還是需要利用 regular expression 做精密的解析。


回C#目錄
回首頁

2012年4月16日 星期一

jquery ui for bootstrap 的使用

最近常常使用 Bootstrap 在做版面配置,頓時覺得 UI 美化是一件很輕鬆的事情,只是要我設計出這種羽量級且各大瀏覽器都通用的 css,那叫我去撞牆會比較快。

可是在我網頁缺少日歷的元件時,才發現他少了這麼個東西,應該是說要用甚麼卻發現沒有才知道沒有,所以我就去 google 了一下,找到一個 Bootstrap 相容的外掛,居然還是 jquery ui,那我就毫不客氣的去引用啦!

果然好用,以下就介紹使用他流程:

1.

首先先到 jquery for bootstrap 下載檔案:

2.

解壓縮出來,將必要的檔案取出來並分類建立路徑 ( 暫以解壓縮出來的檔案路徑為路徑 )

需要的檔案:
  • css/custom-theme/jquery-ui-1.8.16.custom.css
  • bootstrap/bootstrap.css
  • js/jquery-1.6.2.min.js
  • js/jquery-ui-1.8.16.custom.min.js

3.

最後我建立一個 sample 檔案,供大家隨意取用:
<html>
    <head>
        <link type="text/css" rel="stylesheet"
href="css/custom-theme/jquery-ui-1.8.16.custom.css"/>
        <link href="bootstrap/bootstrap.css" rel="stylesheet">
    </head>
    
    <body>
        <div id="datepicker"></div>
    </body>
    
    <script type="text/javascript" 
src="js/jquery-1.6.2.min.js"></script>
    <script type="text/javascript" 
src="js/jquery-ui-1.8.16.custom.min.js"></script>
    
    <script>
        $('#datepicker').datepicker();
    </script>
</html>

回html目錄
回首頁

2012年4月11日 星期三

SQL 自製類別

這個 SQL 類別為我個人設計並撰寫,可能設計的不夠完整,就請廣大的觀眾多多指教囉!

若要引用請註明來源,歡迎取用。







使用範例:
private SQL sql;

protected void Page_Load(object sender, EventArgs e)
{
    sql = new SQL("NorthwindConnectionString", 
        SQLConnectionType.SettingsName);

    Hashtable ht = new Hashtable();
    ht.Add("eID", "1");

    gv.DataSource = sql.ExecuteDataTable(
        "SELECT * FROM Employees WHERE EmployeeID=@eID ", ht);
    gv.DataBind();
    

}

類別程式碼如下:
public enum SQLConnectionType
{
    ConnectionString,
    SettingsName
};

/// <summary>
/// SQL 的摘要描述
/// </summary>
public class SQL
{
    private DataTable dt;

    private SqlConnection connection;
    private SqlCommand command;
    private SqlDataAdapter adapter;
    public SqlDataReader reader { get; set; }
    private SqlTransaction tran;
    public bool isError { get; set; }
    public string errorMessage { get; set; }

    /// <summary>
    /// 
    /// </summary>
    public SQL()
    {

    }

    /// <summary>
    /// 初始化資料庫連線
    /// </summary>
    public SQL(string strConnection, SQLConnectionType type)
    {
        Connection(strConnection, type);
    }

    /// <summary>
    /// 資料庫連線
    /// </summary>
    /// <param name="strConnection">連接字串</param>
    /// <param name="type">資料庫連線方式</param>
    public void Connection(string strConnection, SQLConnectionType type)
    {
        connection = new SqlConnection();
        command = new SqlCommand();

        switch (type)
        {
            case SQLConnectionType.ConnectionString:
                
                connection.ConnectionString = strConnection;
                command.Connection = connection;
                
                break;

            case SQLConnectionType.SettingsName:

                connection.ConnectionString =
                    ConfigurationManager.ConnectionStrings[strConnection].
                    ConnectionString;
                command.Connection = connection;

                break;
        }
    }

    /// <summary>
    /// 執行資料庫語法
    /// </summary>
    /// <param name="query">指令</param>
    public void Execute(string query)
    {
        command.CommandText = query;
        connection.Open();

        isError = false;
        try
        {
            // 開始執行資料庫交易
            tran = connection.BeginTransaction();
            command.Transaction = tran;
            command.ExecuteNonQuery();
            tran.Commit();

        }
        catch (Exception ex)
        {
            // 失敗則 Rollback
            tran.Rollback();

            errorMessage = ex.Message;
            isError = true;
        }
        finally
        {
            connection.Close();
        }
    }

    /// <summary>
    /// 執行資料庫語法
    /// </summary>
    /// <param name="query">指令</param>
    /// <param name="ht">參數</param>
    public void Execute(string query, Hashtable ht)
    {
        SetParameters(ht);
        Execute(query);
    }

    /// <summary>
    /// 傳回結果第一行第一列之資料
    /// </summary>
    /// <param name="query">指令</param>
    /// <returns></returns>
    public object ExecuteScalar(string query)
    {
        object obj = string.Empty;

        command.CommandText = query;
        connection.Open();

        isError = false;
        try
        {
            obj = command.ExecuteScalar();
        }
        catch (Exception ex)
        {
            errorMessage = ex.Message;
            isError = true;
        }
        finally
        {
            connection.Close();
        }

        return obj;
    }

    /// <summary>
    /// 傳回結果第一行第一列之資料
    /// </summary>
    /// <param name="query">指令</param>
    /// <param name="ht">參數</param>
    /// <returns></returns>
    public object ExecuteScalar(string query, Hashtable ht)
    {
        SetParameters(ht);

        return ExecuteScalar(query);
    }


    /// <summary>
    /// 傳回 DataTable
    /// </summary>
    /// <param name="query">指令</param>
    /// <returns></returns>
    public DataTable ExecuteDataTable(string query)
    {
        dt = new DataTable();
        command.CommandText = query;
        
        isError = false;
        try
        {
            adapter = new SqlDataAdapter(command);
            adapter.Fill(dt);
        }
        catch(Exception ex)
        {
            errorMessage = ex.Message;
            isError = true;
        }

        return dt;
    }

    /// <summary>
    /// 傳回 DataTable
    /// </summary>
    /// <param name="query">指令</param>
    /// <param name="ht">參數</param>
    /// <returns></returns>
    public DataTable ExecuteDataTable(string query, Hashtable ht)
    {
        SetParameters(ht);

        return ExecuteDataTable(query);
    }

    /// <summary>
    /// 傳回 SqlDataReader
    /// </summary>
    /// <param name="query">指令</param>
    public SqlDataReader ExecuteReader(string query)
    {
        command.CommandText = query;

        reader = command.ExecuteReader();

        return reader;
    }

    /// <summary>
    /// 傳回 SqlDataReader
    /// </summary>
    /// <param name="query">指令</param>
    /// <param name="ht">參數</param>
    /// <returns></returns>
    public SqlDataReader ExecuteReader(string query, Hashtable ht)
    {
        SetParameters(ht);
        return ExecuteReader(query);
    }

    /// <summary>
    /// 查詢資料是否存在
    /// </summary>
    /// <param name="query">指令</param>
    /// <returns></returns>
    public bool Exists(string query)
    {
        object obj;
        bool isExists = false;
        int iResult = 0;

        command.CommandText = query;
        connection.Open();

        isError = false;
        try
        {
            obj = command.ExecuteScalar();

            if (int.TryParse(Convert.ToString(obj), out iResult))
            {
                if (iResult > 0)
                    isExists = true;
            }
        }
        catch (Exception ex)
        {
            errorMessage = ex.Message;
            isError = true;
        }
        finally
        {
            connection.Close();
        }

        return isExists;
    }

    /// <summary>
    /// 查詢資料是否存在
    /// </summary>
    /// <param name="query">指令</param>
    /// <param name="ht">參數</param>
    /// <returns></returns>
    public bool Exists(string query, Hashtable ht)
    {
        SetParameters(ht);
        return Exists(query);
    }

    /// <summary>
    /// 將資料庫連線開啟或關閉
    /// </summary>
    /// <param name="strStatus">狀態字串(open or close)</param>
    public void CallConnection(string strStatus)
    {
        switch (strStatus.ToLower())
        {
            case "open":
                if (connection.State == ConnectionState.Closed)
                    connection.Open();
                break;
            case "close":
                if (connection.State == ConnectionState.Open)
                    connection.Close();
                break;

        }
    }

    /// <summary>
    /// 設定參數
    /// </summary>
    /// <param name="ht">參數</param>
    private void SetParameters(Hashtable ht)
    {
        command.Parameters.Clear();
        foreach (DictionaryEntry de in ht)
            command.Parameters.AddWithValue(Convert.ToString(de.Key),
            Convert.ToString(de.Value));
    }
}

回C#目錄
回首頁