我如何使用HTML Agility Pack从HTML中提取一行

时间:2019-03-24 20:27:45

标签: c# html datagridview html-agility-pack

亲爱的

我的代码有问题。我想从HTML文件中选择2个xpath。

第一个xpath是:/ html / body / center [2] / table / tbody / tr [2] / td [1] / a [2] / font 下面的HTML代码。我要命名为“ BFhostname”

<font size="+1" face="Tahoma, Arial, Helvetica" color="#ffffcc">BFhostname /font>

第二个xpath是:// html / body / center [2] / table / tbody / tr [2] / td [10] / a / img 删除代码html:<pre>img title="disk:red:9d13h24m" alt="disk:red:9d13h24m" src="red%20%20Xymon%20-%20Status%20@%20Thu%20Jan%2003%20172037%202019_pliki/red.gif" width="16" height="16" border="0"</pre>

在第二种情况下,我只希望掌握以下信息:alt =“ disk:red:9d13h24m”

<i>Hide   Expand   Copy Code

    namespace EvenetViewer
    {

        public partial class Form1 : Form
        {
DataTable table;



            public Form1()
            {
                InitializeComponent();
                InitTable();
            }

            public class nameandalert
            {
                public string Host { get; set; }
                public string Alert { get; set; }
            }
            private void InitTable()
            {
                table = new DataTable("EventViewer");
                table.Columns.Add("Host", typeof(String));
                table.Columns.Add("Alert", typeof(String));
                table.Columns.Add("Time", typeof(String));
                EventviewBB.DataSource = table;
            }

            private void Form1_Load(object sender, EventArgs e)
            {
                //InitTable();


                WebClient webClient = new WebClient();
                string page = webClient.DownloadString(@"C:\temp\test3.htm");
                HtmlAgilityPack.HtmlDocument doc = new HtmlAgilityPack.HtmlDocument();
                doc.LoadHtml(page);
                richTextBox1.Text = page;


                          var namenodes = doc.DocumentNode.SelectNodes("//table[@border='0']/tbody/tr[2]/td[1]/a[2]");
                          var host = namenodes.Select(node => node.InnerText);
    table.Rows.Add(host)
                          var alertnodes = doc.DocumentNode.SelectNodes("//table[@cellpadding=2]/tbody/tr[2]/td[10]/a/");
                          var alert = alertnodes.Select(node => node.InnerText);
                table.Rows.Add(Alert);

           }
    }

    }

0 个答案:

没有答案