如何使用使用HtmlAgilityPack獲取表值

asp.net html-agility-pack html-helper html-parsing

http://www.dsebd.org/latest_PE_all2_08.php

我在asp.net C#web上工作。上面的url包含一些信息,我需要將它們保存在我的數據庫中,還需要在指定的位置保存為xml格式。這個url包含一個表。我想得到這個表值但是如何從這個html表中檢索值。

http://www.dsebd.org/latest_PE_all2_08.php

我需要幫助從這個文檔中獲取表信息。如何保存這個表值。給我一些語法

熱門答案

public partial class WebForm3 : System.Web.UI.Page
    {
        private byte[] aRequestHTML;        
        private string myString = null;     
        protected System.Web.UI.WebControls.Label Label1;     
        private ArrayList a = new ArrayList(); 


        protected void Page_Load(object sender, EventArgs e)
        {
            WebClient objWebClient = new WebClient();

            aRequestHTML = objWebClient.DownloadData("http://www.dsebd.org/latest_PE_all2_08.php");
            // creates UTf8 encoding object
            UTF8Encoding utf8 = new UTF8Encoding();
            // gets the UTF8 encoding of all the html we got in aRequestHTML
            myString = utf8.GetString(aRequestHTML);

            string html = @"
        <html><head></head>
        <body><div>
            <table border=1>
                <tr><td>sno</td><td>sname</td></tr>
                <tr><td>111</td><td>abcde</td></tr>
                <tr><td>213</td><td>ejkll</td></tr>
            </table>
            <table border=1>
                <tr><td>adress</td><td>phoneno</td><td>note</td></tr>
                <tr><td>asdlkj</td><td>121510</td><td>none</td></tr>
                <tr><td>asdlkj</td><td>214545</td><td>none</td></tr>
            </table>
        </div></body>
        </html>";


            HtmlDocument htmlDoc = new HtmlDocument();
            htmlDoc.LoadHtml(myString);


            DataTable addressAndPhones;
            foreach (var table in ParseAllTables(htmlDoc))
            {
                if (table.Columns.Contains("Trading Code") && table.Columns.Contains("Close Price"))
                {
                    // You found the address and phone number table
                    addressAndPhones = table;
                }
            }


        }

        private static DataTable[] ParseAllTables(HtmlDocument doc)
        {
            var result = new List<DataTable>();
            foreach (var table in doc.DocumentNode.Descendants("table"))
            {
                result.Add(ParseTable(table));
            }
            return result.ToArray();
        }

        private static DataTable ParseTable(HtmlNode table)
        {
            var result = new DataTable();

            var rows = table.Descendants("tr");

            var header = rows.Take(1).First();
            foreach (var column in header.Descendants("td"))
            {
                result.Columns.Add(new DataColumn(column.InnerText, typeof(string)));
            }

            foreach (var row in rows.Skip(1))
            {
                var data = new List<string>();
                foreach (var column in row.Descendants("td"))
                {
                    data.Add(column.InnerText);
                }
                result.Rows.Add(data.ToArray());
            }
            return result;
        }

    }

通過這種方式,您可以輕鬆創建DataTable,然後可以保存DataBase。




許可下: CC-BY-SA with attribution
不隸屬於 Stack Overflow
這個KB合法嗎? 是的,了解原因
許可下: CC-BY-SA with attribution
不隸屬於 Stack Overflow
這個KB合法嗎? 是的,了解原因