HTML敏捷性包nextsibling如果有标签之间的空格没有找到元素(Html Agility P

2019-09-30 06:51发布

我试图找到一个特定的标签的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值需要内部消除去除标签之间的所有空格?

文章来源: Html Agility Pack nextsibling not finding element if there are white spaces between tags