Html Agility Pack nextsibling not finding element if there are white spaces between tags

房东的猫 提交于 2019-12-11 07:15:59

问题


I'm trying to find the nextSibling innerText of a specific tag, but I can't get the proper value when I try to parse an html string which contains white spaces or new lines

here is my code:

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;

}

Can I get nextSibling value whithout requiring to remove all whitespaces between tags?

来源:https://stackoverflow.com/questions/17785682/html-agility-pack-nextsibling-not-finding-element-if-there-are-white-spaces-betw

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!