2011-01-28 3 views
0

私の関数の前半はhtmlagilitypackを使用していません。関数は後半で何もせずに終了し、エラーを返さない。あなたは多分あなたはHtmlDocument.DocumentNode.OuterXmlDocument.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

ごめんなさい。 1)デバッグはコンパイルされていますか?あなたのプロジェクトとhtmlagilitypackの両方?それ以外の場合は、デバッグ時にコードをジャンプするように見えます。 2)ブラックリストを使用しないでください。他の賢明なあなたが*志望する*ホワイトリストを使用してください! – stefan

+0

また、文字列連結( '+')ではなく 'StringBuilder'を使用してください - このようなループは' StringBuilder'の理想的なシナリオです –

+0

あなたがデバッグするとそこに行きますか? 'myDocumentText(html);'が何か面倒なことをしているのだろうか? 'myDocumentText(html);'への最初の呼び出しを削除して、最後に残しておけばどうなるでしょうか? –

答えて

0

HtmlDocument.ToString()は、テキストを返送しないのですか?助けてください

0
myDocumentText(html); 

このメソッドは何をしますか?

私の前提は、このメソッドのどこかに例外がスローされていて、それが飲み込まれているか、デバッグ環境がユーザーがスローした例外を破棄しないように設定されていることです。

この方法でコードを投稿できますか?

関連する問題