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

发布于 2024-10-14 16:54:21 字数 1751 浏览 6 评论 0原文

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


    }

The first half of my function doesn't use htmlagilitypack and I know it functions as I want. however the function finishes without doing anything with the second half and doesnt return an errors. Please help

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


    }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

人间☆小暴躁 2024-10-21 16:54:21

HtmlDocument.ToString() 不会发回文本,除非您更改了原始代码,也许您正在寻找 HtmlDocument.DocumentNode.OuterXmlDocument。保存(...文本...)

HtmlDocument.ToString() does not send back the text, unless you changed the original code, maybe you're looking for HtmlDocument.DocumentNode.OuterXml or Document.Save( ... text ...)?

草莓酥 2024-10-21 16:54:21
myDocumentText(html);

这个方法有什么作用?

我的假设是,您在此方法中的某处抛出了异常,并且该异常要么被吞掉,要么您的调试环境设置为不会因用户抛出的异常而中断。

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

myDocumentText(html);

What does this method do?

My assumption is that you have an exception being thrown somewhere within this method, and it's either being swallowed, or your debug environment is set to not break on user thrown exceptions.

Can you post the code within this method?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文