私はHTMLAgilityPackライブラリに関して良いことを聞いたので、試してみたいと思っていましたが、私はそれに全く成功しませんでした。私はこれを数ヶ月間把握しようとしてきました。私が何をしていても、このコードではnull以外のものを私に与えることはできません。私はこの例(http://www.c-sharpcorner.com/uploadfile/9b86d4/getting-started-with-html-agility-pack/)に従ってみましたが、私は同じ結果を得ていません。理由を説明できません。HTMLAgilityPack選択ノードは常にnullを返します
ファイルをロードしてからSelectNodesを実行してすべてのハイパーリンクを選択しようとしますが、常に空のリストが返されます。私はすべての種類のノード(div、p、a、すべてと何か)を選択しようとしましたが、常に空のリストを返します。私はdoc.Descendantsを使ってみました、私はローカルとウェブ上のさまざまなソースファイルを使用しようとしましたが、私は何も実際の結果を返すことはありません。
私は何か重要なことを見落とさなければなりませんでしたが、私はそれが何であるか把握できません。私は何が欠けていますか?
コード:
public string GetSource()
{
try
{
string result = "";
HtmlAgilityPack.HtmlDocument doc = new HtmlAgilityPack.HtmlDocument();
if (!System.IO.File.Exists("htmldoc.html"))
throw new Exception("Unable to load doc");
doc.LoadHtml("htmldoc.html"); // copied locally to bin folder, confirmed it found the file and loaded it
HtmlNodeCollection nodes = doc.DocumentNode.SelectNodes("//a"); // Always returns null, regardless of what I put in here
if (nodes != null)
{
foreach (HtmlNode item in nodes)
{
result += item.InnerText;
}
}
else
{
// Every. Single. Time.
throw new Exception("No matching nodes found in document");
}
return result;
}
catch (Exception ex)
{
return ex.ToString();
}
}
ソースHTMLファイル 'htmldoc.html' 私はこのようになります使用しています:あなたはLoad
メソッドを使用する必要があるファイルをロードするには
<html>
<head>
<title>Testing HTML Agility Pack</title>
</head>
<body>
<div id="div1">
<a href="div1-a1">Link 1 inside div1</a>
<a href="div1-a2">Link 2 inside div1</a>
</div>
<a href="a3">Link 3 outside all divs</a>
<div id="div2">
<a href="div2-a1">Link 1 inside div2</a>
<a href="div2-a2">Link 2 inside div2</a>
</div>
</body>
</html>
それは簡単でした。これで解決しました。 –