How to get node specific value from HTML source

Sep 25, 2012 at 12:08 PM

Hi everyone, I can't seem to retrieve the value of a node using the function htmldoc.DocumentNode.SelectNodes("xpath path").
I would like to read the value of the tag in the example at the end of the source <font color 'mediumseagreen' >BUY-IF</font>.

I tried passing to the function the path /html/body/table/tbody/tr/td/table[5]/tbody/tr/td/table/tbody/tr/td[4]/table/tbody/tr[2]/td/table/tbody/tr/td/font but the function return null.
Someone can help me?

Thanks

C# CODE

public static HtmlDocument ParseHtml(string URL)
        {
            HtmlDocument hDoc = new HtmlDocument();
            try
            {
                WebClient wClient = new WebClient();
                byte[] bData = wClient.DownloadData(URL);
                hDoc.LoadHtml(ASCIIEncoding.ASCII.GetString(bData));
            }
            catch
            {
                hDoc.LoadHtml("");
            }
            return hDoc;
        }

HtmlAgilityPack.HtmlDocument htmldoc = ParseHtml(url);

HtmlNode hNode = htmldoc.DocumentNode.SelectNodes("/html/body/table/tbody/tr/td/table[5]/tbody/tr/td/table/tbody/tr/td[4]/table/tbody/tr[2]/td/table/tbody/tr/td/font")

HTML SOURCE

<html>
<head>
<meta http-equiv="Content-Type" content="text/html;CHARSET=utf-8">
<script language="JavaScript" type="text/javascript">
function preload() {
  if (!document.images) return;
  var ar = new Array();
  var arguments = preload.arguments;
  for (var i = 0; i < arguments.length; i++) {
    ar[i] = new Image();
    ar[i].src = arguments[i];
  }
}
</script>
<style type="text/css"><!--
BODY {font-size:10pt;font-family: Verdana,Arial,Helvetica;}
TD {font-size:10pt;font-family: Verdana,Arial,Helvetica;}
.normalloose {font-size:10pt;line-height:150%;}
TH {color:white; background:#336666; font-weight:bold;font-size:8.5pt;}
--></style>
<script language="JavaScript" type="text/JavaScript">
<!--
function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}
function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}
//-->
</script>
<title>BMPS.MI&nbsp;&nbsp;BANCA MONTE DEI PASCHI DI SIENA ORD&nbsp;&nbsp;&nbsp;&nbsp;Borsa Italiana&nbsp;&nbsp;FTSE MIB </title>
<meta name="Description" content="Analysis of BMPS.MI, BANCA MONTE DEI PASCHI DI SIENA ORD, Milan, , in the Stock Markets of Italia with End-of-Day Stock Data, Alert Signals: Buy, Sell, Hold, Japanese Candlesticks, Reversal Patterns, Daily Commentary and Support.">
<meta name="Keywords" content="BMPS.MI, BANCA MONTE DEI PASCHI DI SIENA ORD, Milan, BMPS.MI, BANCA MPS, ,   Daily, Stock, Exchange,  Italia , Milan ,  candlestick, signal, stock, market, security, reversal, continuation, bullish, bearish, chart, pattern, technical, analysis">
</head>
<body bgcolor="SlateGray" topmargin="15" onload="MM_preloadImages('images/DBHOME.gif','images/DBSUBSCRIBE.gif','images/DBLOGIN.gif','images/DBSUPPORT.gif','images/DBPRIVACY.gif','images/LBHOME.gif','images/LBSUBSCRIBE.gif','images/LBLOGIN.gif','images/LBSUPPORT.gif','images/LBPRIVACY.gif')">
<table width="942" border="1" align="center" cellpadding="0" cellspacing="0" bgcolor="#FFFFFF">
    <tr>
        <td width="940" valign="top">
            <table width="940" height="100" border="0" cellpadding="0" cellspacing="0" bgcolor="00008b">
                <tr>
                      <td width="191">
                          <table align="center" width="191" height="100" border="0" cellpadding="0" cellspacing="0" bgcolor="00008b">
                            <tr>
                                  <td colspan="4" height="85" align="center" valign="middle"><span class="normalloose"><a href="Default.asp"><img src="images/Italianbullstitle.gif" border="0"></a></span>
                                </td>
                              </tr>
                              <tr>
                                  <td align="left" width="57" valign="bottom"><img src="images/Dailyon.gif"></td>
                                  <td align="left" width="18" valign="bottom"><img src="images/DailyonMid.gif"></td>
                             
                                  <td align="left" width="75" valign="bottom"><a href="weekly/StockPage.asp?CompanyTicker=BMPS.MI&MarketTicker=Milan&Typ=S" onmouseover="MM_swapImage('Image700','','images/Weeklyoff1.gif',1)" onmouseout="MM_swapImgRestore()"><img src="images/Weeklyoff.gif" alt="To the weekly page" name="Image700" border="0"></a></td>
                            
                                <td align="left" width="41" valign="bottom"></td>
                              </tr>
                        </table>
                    </td>
                    <td width="749" align="right" valign="middle">
                        <table width="730" height="92" border="0" cellpadding="0" cellspacing="0">
                            <tr>
                                <td align="center" valign="middle">
                          
<script type="text/javascript"><!--
google_ad_client = "ca-pub-1222611613413645";
/* 728x90Leaderboard */
google_ad_slot = "7452747120";
google_ad_width = 728;
google_ad_height = 90;
//-->
</script>
<script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script>
                                </td>
                              </tr>
                           </table>
                      </td>
                </tr>
              </table>
              <table width="940" height="2" border="0" cellpadding="0" cellspacing="0" bgcolor="#C0C0C0">
        
                  <tr>
                    <td></td>
                </tr>
              </table>
           
              <table width="940" border="0" cellpadding="0" cellspacing="0" bgcolor="#C0C0C0">
                    <tr>
                      <td width="70" align="center" valign="middle"><a href="Default.asp" onmouseout="MM_swapImgRestore()" onmouseover="MM_swapImage('Image32','','images/LBHOME.gif',1)"><img src="images/DBHOME.gif" alt="Home" name="Image32" border="0"></a></td>
                      <td width="70" align="center" valign="middle"><a href="https://www.paymentbulls.com/euro/MembershipNew.asp?CountrySymbol=Italian" onmouseout="MM_swapImgRestore()" onmouseover="MM_swapImage('Image13','','images/LBSUBSCRIBE.gif',1)"><img src="images/DBSUBSCRIBE.gif" alt="Subscribe" name="Image13" border="0"></a></td>
                      <td width="70" align="center" valign="middle"><a href="Login.asp" onmouseout="MM_swapImgRestore()" onmouseover="MM_swapImage('Image14','','images/LBLOGIN.gif',1)"><img src="images/DBLOGIN.gif" alt="Login" name="Image14" border="0"></a></td>
                      <td width="70" align="center" valign="middle"><a href="Support.asp" onmouseout="MM_swapImgRestore()" onmouseover="MM_swapImage('Image15','','images/LBSUPPORT.gif',1)"><img src="images/DBSUPPORT.gif" alt="Support" name="Image15" border="0"></a></td>          
                       <td width="70" align="center" valign="middle"><a href="Privacy.asp" onmouseout="MM_swapImgRestore()" onmouseover="MM_swapImage('Image16','','images/LBPRIVACY.gif',1)"><img src="images/DBPRIVACY.gif" alt="Privacy" name="Image16" border="0"></a></td>
                      <td width="70" align="center" valign="middle"><a href="StockList.asp" onmouseout="MM_swapImgRestore()" onmouseover="MM_swapImage('Image17','','images/LBStockList.gif',1)"><img src="images/DBStockList.gif" alt="Stock List" name="Image17" border="0"></a></td>
                    <td width="70" align="center" valign="middle"><a href="IndexList.asp" onmouseout="MM_swapImgRestore()" onmouseover="MM_swapImage('Image18','','images/LBIndexList.gif',1)"><img src="images/DBIndexList.gif" alt="Index List" name="Image18" border="0"></a></td>
                      <td width="70" align="right" valign="middle"><font size="1" color="DarkGreen">Search</font></td>
                      <form action="StockFind.asp" method="post" id="form1" name="form1">
                      <td width="140" align="center" valign="middle"><strong>
                      <input size="15" maxlength="50" type="text" id="zCompanyTicker" name="zCompanyTicker"></strong>
                      </td>
                      <td width="30" align="center" valign="middle"><strong>
                      <input type="image" src="images/GBUTTON.gif" id="submit1" name="submit1" width="24" height="24"></strong>
                      </td>
                      </form>
                      <td width="200" align="center" valign="middle"><font color="#CC0033">

            <font color="red" size="1">
            Last Update
            24/9/2012
            </font>
       
                    </font>
                    </td>
                   
                    <td width="43" align="right" valign="middle"><strong><font size="1">
                        25/9/2012
                            </font><font color="#006600"></font></strong></td>
                    <td width="7" align="center" valign="middle">&nbsp;
                    </td>
                </tr>
            </table>
            <table width="940" height="2" border="0" cellpadding="0" cellspacing="0" bgcolor="#C0C0C0">
                <tr>
                    <td>
                    </td>
                </tr>
            </table>
            <table width="940" valign="top" border="0" cellpadding="0" cellspacing="0">
                <tr>
                      <td valign="top">
                          <table valign="top" height="350" border="0" cellpadding="0" cellspacing="0">
                            <tr valign="top">
                                  <td width="14">&nbsp; </td>
                                  <td width="310">
                                      <table width="295" height="120" border="0" cellspacing="0" cellpadding="0">
                                          <tr>
                                            <td colspan="5" valign="middle" height="10">
                                            </td>
                                           </tr>
                                        <tr>
                                              <td width="10" height="40" bgcolor="#333333" valign="middle"></td>
                                              <td width="40" height="40" align="center" bgcolor="#333333"><img src="images/ItalianFLAG01.gif"></td>
                                            <td width="245" align="left" bgcolor="#333333">
                                                <table width="100%" align="left" height="100%" border="0" cellspacing="0" cellpadding="0" bgcolor="#333333">
                                                    <tr align="center">
                                                        <td>
                                                            <font size="5" color="white"><b>
                                                            BMPS.MI
                                                            </b></font>
                                                        </td>
                                                    </tr>
                                                </table>
                                            </td>
                                        </tr>
                                        <tr valign="middle">
                                            <td height="40" colspan="5"><font size="2"><b>
                                                    BANCA MONTE DEI PASCHI DI SIENA ORD
                                                 </b>
                                                 <br>
                                                 FTSE MIB&nbsp;&nbsp;
                                                    </font>
                                            </td>
                                        </tr>
                                      </table>
                                    <table width="295" border="0" cellpadding="0" cellspacing="0">
                                          <tr>
                                            <td width="295" height="141" valign="top">
                                                <table width="100%" border="0" cellpadding="0" cellspacing="0">
                                                    <tr>
                                                        <td height="35" valign="top"><font size="2"><b>Daily Commentary</b></font><br><br>
                                            <div style="font-size:9px;font-family:verdana"> 
                                            <p align="justify">
                                               Our system posted a 
                                        <font color="mediumseagreen">BUY-IF</font>

....