使用HtmlAgilityPack獲取嵌套表格單元格

c# html-agility-pack html-parsing

我有以下html頁面:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>

<head>
</head>

<body>
<div style="LEFT: 135; WIDTH: 450; POSITION: absolute; TOP: 70">
  <a name="content_begins"></a>
  <table cellSpacing="0" cellPadding="0" width="100%" border="0">
    <tr>
      <td vAlign="top">
      <table class="PortalGadget" cellSpacing="0" cellPadding="0" width="100%" border="0">
        <tr>
          <td>
          <table cellSpacing="6" cellPadding="0" border="0">
            <form name="authenticator" ACTION="auth-cup" METHOD="POST" ENCTYPE="application/x-www-form-urlencoded">
              <tr bgColor="#6c8899">
                <td><font face="Trebuchet MS" color="#ffffff">&nbsp;*** You Have Already Logged In ***</font></td>
              </tr>

              <tr height="20">
                <td vAlign="bottom" height="20">
                <font face="Trebuchet MS" size="-1">Context:</font></td>
              </tr>
              <tr>
                <td></td>
              </tr>
              <tr>
                <td vAlign="bottom" height="20">
                <font face="Trebuchet MS" size="-1">Username:</font></td>
              </tr>
              <tr>
                <td><input size="40" name="username"></td>
              </tr>
              <tr>
                <td><font face="Trebuchet MS" size="-1">Password:</font></td>
              </tr>
              <tr>
                <td><input type="password" size="40" name="password"></td>
              </tr>
              <tr>
                <td vAlign="bottom" height="20">
                <font face="Trebuchet MS" size="-1">Destination:</font></td>
              </tr>
              <tr>
                <td>
                <input size="40" name="url" value="http://ya.ru/"></td>
              </tr>
              <tr>
                <!---- Hidden field to identify forward/reverse proxy path ------>
                <input TYPE="hidden" NAME="proxypath" VALUE="forward">
              </tr>
              <tr>
                <td vAlign="bottom" align="left">
                <input type="submit" alt="Login" value="Login" border="0" name="loginButton">&nbsp;&nbsp;
                <input type="reset" alt="Reset" value="Reset"></td>
              </tr>
              <tr height="5">
                <td height="5"><hr SIZE="2"></td>
              </tr>
              <tr>
                <td><font face="Trebuchet MS" size="-1">
                <p>Copyright 1999-2003 Novell, Inc. All rights reserved.</p>
                </font></td>
              </tr>
            </form>
            </tbody>
          </table>
          </td>
        </tr>
      </table>
      </td>
    </tr>
  </table>
</div>
</body>
</html>

我想從td獲得“你已經登錄”的文字。我有這個代碼:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>

<head>
</head>

<body>
<div style="LEFT: 135; WIDTH: 450; POSITION: absolute; TOP: 70">
  <a name="content_begins"></a>
  <table cellSpacing="0" cellPadding="0" width="100%" border="0">
    <tr>
      <td vAlign="top">
      <table class="PortalGadget" cellSpacing="0" cellPadding="0" width="100%" border="0">
        <tr>
          <td>
          <table cellSpacing="6" cellPadding="0" border="0">
            <form name="authenticator" ACTION="auth-cup" METHOD="POST" ENCTYPE="application/x-www-form-urlencoded">
              <tr bgColor="#6c8899">
                <td><font face="Trebuchet MS" color="#ffffff">&nbsp;*** You Have Already Logged In ***</font></td>
              </tr>

              <tr height="20">
                <td vAlign="bottom" height="20">
                <font face="Trebuchet MS" size="-1">Context:</font></td>
              </tr>
              <tr>
                <td></td>
              </tr>
              <tr>
                <td vAlign="bottom" height="20">
                <font face="Trebuchet MS" size="-1">Username:</font></td>
              </tr>
              <tr>
                <td><input size="40" name="username"></td>
              </tr>
              <tr>
                <td><font face="Trebuchet MS" size="-1">Password:</font></td>
              </tr>
              <tr>
                <td><input type="password" size="40" name="password"></td>
              </tr>
              <tr>
                <td vAlign="bottom" height="20">
                <font face="Trebuchet MS" size="-1">Destination:</font></td>
              </tr>
              <tr>
                <td>
                <input size="40" name="url" value="http://ya.ru/"></td>
              </tr>
              <tr>
                <!---- Hidden field to identify forward/reverse proxy path ------>
                <input TYPE="hidden" NAME="proxypath" VALUE="forward">
              </tr>
              <tr>
                <td vAlign="bottom" align="left">
                <input type="submit" alt="Login" value="Login" border="0" name="loginButton">&nbsp;&nbsp;
                <input type="reset" alt="Reset" value="Reset"></td>
              </tr>
              <tr height="5">
                <td height="5"><hr SIZE="2"></td>
              </tr>
              <tr>
                <td><font face="Trebuchet MS" size="-1">
                <p>Copyright 1999-2003 Novell, Inc. All rights reserved.</p>
                </font></td>
              </tr>
            </form>
            </tbody>
          </table>
          </td>
        </tr>
      </table>
      </td>
    </tr>
  </table>
</div>
</body>
</html>

但是我無法在代碼中獲得“你已經登錄過”的文字。如何在此方案中獲取嵌套表格單元格值?

一般承認的答案

嗯,我發現解決方案在我看來:

string value = doc.DocumentNode.SelectNodes("//table[@class='PortalGadget']/tr/td/table/tr/td")[0].InnerText;

熱門答案

要從另一個節點中選擇節點,您必須放置一個“。”在XPath的開頭。

所以table.SelectNodes("//table")變成table.SelectNodes(".//table")




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