【发布时间】: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