【问题标题】:Why is my function just skipping over the code that uses HtmlAgilityPack?为什么我的函数只是跳过使用 HtmlAgilityPack 的代码?
【发布时间】:2011-01-28 05:04:07
【问题描述】:

我的函数的前半部分没有使用 htmlagilitypack,我知道它可以按我的意愿运行。但是该函数在后半部分没有做任何事情就完成了,并且不返回错误。请帮忙

void classListHtml()
    {

        HtmlElementCollection elements = browser.Document.GetElementsByTagName("tr");
        html = "<table>";
        int i = 0;
        foreach (HtmlElement element in elements)
        {
            if (element.InnerHtml.Contains("Marking Period 2") && i != 0)//will be changed to current assignment reports later
            {
                html += "" + element.OuterHtml;
            }
            else if (i == 0)
            {
                i++;
                continue;
            }
            else
                continue;

        }
        html += "" + "</table>";
        myDocumentText(html);


        //---------THIS IS WHERE IT STOPS DOING WHAT I WANT-----------
        //removing color and other attributes
        HtmlAgilityPack.HtmlDocument doc = new HtmlAgilityPack.HtmlDocument();
        doc.Load(html);
        HtmlNodeCollection nodeCollection = doc.DocumentNode.SelectNodes("//tr");//xpath expression for all row nodes
        string[] blackListAttributes={"width", "valign","bgcolor","align","class"};

        foreach(HtmlNode node in nodeCollection)//for each row node
        {
            HtmlAttributeCollection rows = node.Attributes;// the attributes of each row node

            foreach (HtmlAttribute attribute in rows)//for each attribute
            {
                if (blackListAttributes.Contains(attribute.Name))//if its attribute name is in the blacklist, remove it.
                    attribute.Remove();
            }
        }

        html = doc.ToString();
        myDocumentText(html);//updating browser with new html


    }

【问题讨论】:

  • 对不起,我暂时离开了,但是; 1)它是调试编译的吗?你的项目和htmlagilitypack?否则在调试时它似乎会跳过代码。 2) 不要使用黑名单。使用白名单,否则您是自愿的!
  • 另外,请确保使用StringBuilder,而不是字符串连接 (+) - 这样的循环是 StringBuilder 的理想场景
  • 如果你调试它,你到了吗?我想知道myDocumentText(html); 是不是在做一些令人讨厌的事情——如果你删除对myDocumentText(html); 的第一个调用,只留下最后一个调用会发生什么?

标签: c# html-agility-pack


【解决方案1】:

HtmlDocument.ToString() 不会发回文本,除非您更改了原始代码,否则您可能正在寻找HtmlDocument.DocumentNode.OuterXmlDocument.Save( ... text ...)

【讨论】:

    【解决方案2】:
    myDocumentText(html);
    

    这个方法有什么作用?

    我的假设是您在此方法中的某个地方抛出了一个异常,并且它被吞没了,或者您的调试环境设置为不会因用户抛出的异常而中断。

    你能在这个方法中发布代码吗?

    【讨论】:

      猜你喜欢
      • 2015-06-03
      • 1970-01-01
      • 2017-09-19
      • 1970-01-01
      • 1970-01-01
      • 2019-05-10
      • 2022-08-14
      • 1970-01-01
      • 2021-08-06
      相关资源
      最近更新 更多