如果标签之间有空格,则Html Agility Pack nextsibling找不到元素

时间:2013-07-22 10:52:16

标签: c# xpath html-agility-pack

我正在尝试查找特定标记的nextSibling innerText,但是当我尝试解析包含空格或新行的html字符串时,我无法获得正确的值

这是我的代码:

private string getTableTdValue()
{
    /*If I strip all white space between tag I get proper results
    string myHtml =
        "<td align='right' width='186'>Text1</td><td align='center' width='51'>Here the result I want to get</td><td width='186'>Text2</td>";*/

    string myHtml =
        @"
        <td align='right' width='186'>Text1</td>
        <td align='center' width='51'>Here the result I want to get</td>
        <td width='186'>Text2</td>";

    HtmlDocument doc = new HtmlDocument();
    HtmlNodeCollection cols = doc.DocumentNode.SelectNodes("//td[@width='186']");

    var tdValue = string.Empty;
    foreach (HtmlNode col in cols)
    {
        if (col.InnerText == "Text1")
        {
            tdValue = col.NextSibling.InnerText;            
        }
    }

    return tdValue;

}

我是否可以获得nextSibling值而无需删除标签之间的所有空格?

0 个答案:

没有答案