2011-01-04 4 views
2

初めてHTMLアジリティパックを試しています。私は、HTMLのURLを解析するためのサンプルコードセクションを使用しています。しかし、私はなぜ私がそれを取得しているかわからないエラーが発生しています。私が間違っていることを誰かが指摘できますか?ここでHTMLアジャイルパックのエラー

は(HTMLはHTMLの入ってくる文字列である)ソースです:

StringBuilder sb = new StringBuilder(); 

HtmlDocument htmldoc = new HtmlDocument(); 
htmldoc.LoadHtml(html); 

foreach (HtmlNode link in htmldoc.DocumentNode.SelectNodes("//a[@HREF]")) 
    { 
    HtmlAttribute att = link.Attributes["HREF"]; 
    sb.AppendLine(att.Value + "|"); 
    } 
return sb.ToString(); 

私は私のアプリ(デバッガは右の「foreachの」後にそれを置く)デバッグするとき、私は、次のエラーが発生します:

System.NullReferenceException was unhandled 
    Message=Object reference not set to an instance of an object. 
    Source=ScreenScraper 
    StackTrace: 
     at ScreenScraper.its.GetITSLoadID(String html) in C:\Web_Projects\ScreenScaper\ScreenScraper\its.cs:line 22 
     at ScreenScraper.frm1.btnStartScraping_Click(Object sender, EventArgs e) in C:\Web_Projects\ScreenScaper\ScreenScraper\frm1.cs:line 43 
     at System.Windows.Forms.Control.OnClick(EventArgs e) 
     at System.Windows.Forms.Button.OnClick(EventArgs e) 
     at System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent) 
     at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks) 
     at System.Windows.Forms.Control.WndProc(Message& m) 
     at System.Windows.Forms.ButtonBase.WndProc(Message& m) 
     at System.Windows.Forms.Button.WndProc(Message& m) 
     at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m) 
     at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m) 
     at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam) 
     at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg) 
     at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(IntPtr dwComponentID, Int32 reason, Int32 pvLoopData) 
     at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context) 
     at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context) 
     at System.Windows.Forms.Application.Run(Form mainForm) 
     at ScreenScraper.Program.Main() in C:\Web_Projects\ScreenScaper\ScreenScraper\Program.cs:line 18 
     at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args) 
     at System.AppDomain.nExecuteAssembly(RuntimeAssembly assembly, String[] args) 
     at System.Runtime.Hosting.ManifestRunner.Run(Boolean checkAptModel) 
     at System.Runtime.Hosting.ManifestRunner.ExecuteAsAssembly() 
     at System.Runtime.Hosting.ApplicationActivator.CreateInstance(ActivationContext activationContext, String[] activationCustomData) 
     at System.Runtime.Hosting.ApplicationActivator.CreateInstance(ActivationContext activationContext) 
     at System.Activator.CreateInstance(ActivationContext activationContext) 
     at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssemblyDebugInZone() 
     at System.Threading.ThreadHelper.ThreadStart_Context(Object state) 
     at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean ignoreSyncCtx) 
     at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state) 
     at System.Threading.ThreadHelper.ThreadStart() 
    InnerException: 
+0

おそらくリストの作成に問題があります。ちょっとした提案ですが、foreachの前にHtmlNodesのListを初期化してから、作成したリストを反復処理してみてください。少なくともそれはそれがループかそれを引き起こしているリストかどうか見ることができるでしょう – bakoyaro

答えて

2

Html Agility Packには、空のコレクションにnullを返す「デザインバグ」があります。だから、あなたの代わりにこれを実行する必要があります。

HtmlNodeList list = htmldoc.DocumentNode.SelectNodes("//a[@HREF]"); 
if (list != null) 
{ 
    foreach (HtmlNode link in list) 
    ... 
} 

ところで

は、XPath式で指定されているすべてのタグは、HTMLは大文字と小文字が区別されますので、それらが(違ったHTMLテキストの中で宣言されていても、小文字でなければなりません、デフォルトのHTML Agility Pack XPATHの規則は、小文字のタグを使用することです)。ですので、代わりに次のように記述してください:

HtmlNodeList list = htmldoc.DocumentNode.SelectNodes("//a[@href]"); 
+0

知識ありがとう!これは完全に機能しました。 – WildBill

+0

これに続く1つのフォローアップの質問。 SelectNodeではなく、特定の「SelectNode」を探していたらどうでしょうか?私はまだそれをリストに入れて評価しますか? – WildBill

+0

@Wildbill - SelectNodeは何も見つからなかった場合(標準の.NETルールと一致している場合)、選択したノードを返します。 –

関連する問題