为什么我的函数只是跳过使用HtmlAgilityPack的代码?

时间:2011-01-28 05:04:07

标签: c# html-agility-pack

我的函数的前半部分没有使用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


    }

2 个答案:

答案 0 :(得分:0)

除非您更改了原始代码,否则

HtmlDocument.ToString()不会发回文本,也许您正在寻找HtmlDocument.DocumentNode.OuterXmlDocument.Save( ... text ...)

答案 1 :(得分:0)

myDocumentText(html);

这种方法有什么作用?

我的假设是你在这个方法的某个地方抛出一个异常,它被吞下,或者你的调试环境设置为不会因用户引发的异常而中断。

您可以在此方法中发布代码吗?

相关问题