私の関数の前半はhtmlagilitypackを使用していません。関数は後半で何もせずに終了し、エラーを返さない。あなたは多分あなたはHtmlDocument.DocumentNode.OuterXml
やDocument.Save(... text ...)
を探している、元のコードを変更しない限り、なぜ私の関数は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)ブラックリストを使用しないでください。他の賢明なあなたが*志望する*ホワイトリストを使用してください! – stefan
また、文字列連結( '+')ではなく 'StringBuilder'を使用してください - このようなループは' StringBuilder'の理想的なシナリオです –
あなたがデバッグするとそこに行きますか? 'myDocumentText(html);'が何か面倒なことをしているのだろうか? 'myDocumentText(html);'への最初の呼び出しを削除して、最後に残しておけばどうなるでしょうか? –