如果您认为 HTML 将与 XML 兼容,则可以使用如下所示的 XML 解析器(使用 XPath)。就个人而言,我喜欢避免使用 HTML 解析器,因为它们又大又复杂。就像使用电锯将树枝折成两半一样。有时,别无他法,但如果有更简单的解决方案,请先尝试。
相关代码片段:
var l_contentCell = l_navigator.SelectSingleNode( "//td[preceding-sibling::td/text()='Test Row Two']" );
完整源代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;
using System.Xml.XPath;
namespace XmlSandbox {
class Program {
static void Main( string[] args ) {
string l_xmlLiteral =
"<html>\n" +
" <head>\n" +
" <title>Test Table Page</title>\n" +
" </head>\n" +
" <body>\n" +
" <table border=\"1\" cellpadding=\"3\" cellspacing=\"5\">\n" +
" <tr>\n" +
" <td>Test Row One</td>\n" +
" <td>Test Content</td>\n" +
" </tr>\n" +
" <tr>\n" +
" <td>Test Row Two</td>\n" +
" <td>Test Content</td>\n" +
" </tr>\n" +
" <tr>\n" +
" <td>Test Row Three</td>\n" +
" <td>Test Content</td>\n" +
" </tr>\n" +
" </table>\n" +
" </body>\n" +
"</html>";
var l_document = XDocument.Parse( l_xmlLiteral );
var l_navigator = l_document.CreateNavigator();
var l_contentCell = l_navigator.SelectSingleNode( "//td[preceding-sibling::td/text()='Test Row Two']" );
Console.WriteLine( l_contentCell.Value );
}
}
}