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

C# 为什么我的函数只是跳过了使用HtmlAgilityPack的代码?,c#,html-agility-pack,C#,Html Agility Pack,我的函数的前半部分没有使用htmlagilitypack,我知道它可以按照我的要求运行。但是,函数在完成后没有对后半部分执行任何操作,并且不会返回错误。请帮忙 void classListHtml() { HtmlElementCollection elements = browser.Document.GetElementsByTagName("tr"); html = "<table>"; int i = 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


    }
void classListHtml()
{
HtmlElementCollection元素=browser.Document.GetElementsByTagName(“tr”);
html=“”;
int i=0;
foreach(元素中的HtmlElement元素)
{
if(element.InnerHtml.Contains(“标记期间2”)&&i!=0)//将在以后更改为当前分配报告
{
html+=“”+element.OuterHtml;
}
else如果(i==0)
{
i++;
持续
}
其他的
持续
}
html++=”+”;
myDocumentText(html);
//---------这就是它停止做我想做的事情的地方-----------
//删除颜色和其他属性
HtmlAgilityPack.HtmlDocument doc=新的HtmlAgilityPack.HtmlDocument();
文档加载(html);
HtmlNodeCollection nodeCollection=doc.DocumentNode.SelectNodes(“//tr”);//所有行节点的xpath表达式
字符串[]blackListAttributes={“宽度”、“有效”、“bgcolor”、“对齐”、“类”};
foreach(nodeCollection中的HtmlNode节点)//对于每行节点
{
HtmlAttributeCollection rows=node.Attributes;//每个行节点的属性
foreach(行中的HtmlAttribute属性)//对于每个属性
{
if(blacklisttributes.Contains(attribute.Name))//如果其属性名在黑名单中,请将其删除。
属性。移除();
}
}
html=doc.ToString();
myDocumentText(html);//使用新的html更新浏览器
}

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

这个方法有什么作用

我的假设是,您在这个方法的某个地方抛出了一个异常,它要么被吞没,要么您的调试环境被设置为不中断用户抛出的异常


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

对不起,我暂时不在这里,但是;1) 它是调试编译的吗?您的项目和htmlagilitypack?否则,它在调试时似乎会跳过代码。2) 不要使用黑名单。使用白名单,否则你会被说服的!另外,请确保您使用的是
StringBuilder
,而不是字符串串联(
+
)-这样的循环是
StringBuilder
的理想方案如果您调试到它中,您能做到吗?我想知道
myDocumentText(html)
正在做一些令人讨厌的事情-如果删除对
myDocumentText(html)的第一个调用,会发生什么,只是把那个留在最后?
myDocumentText(html);