目次テーブル id = "toc" からリンクを取得し、それぞれをメソッドに渡します。メソッド内で、ブックマーク (Span タグ) に続いてテーブルにアクセスしようとしていますが、コードは NextSibling と NextElementSibling に対して null を返し続けます。以下は私のhtmlと私のC#コードです。
<table id="toc" class="toc">
<tr>
<td>
<div id="toctitle">
<h2>Contents</h2>
</div>
<ul>
<li class="toclevel-1 tocsection-1"><a href="#DEV_Environment"><span class="tocnumber">1</span> <span class="toctext">DEV Environment</span></a>
</li>
</ul>
</td>
</tr>
</table>
<h2> <span class="mw-headline" id="DEV_Environment"> DEV Environment </span></h2>
<table border="1" cellpadding="2">
<tr>
<th scope="col" style="width: 225px; background: #6EB4EB">CDR IP
</th>
<th scope="col" style="width: 225px; background: #6EB4EB">Client FEP IP:Port
</th>
<th scope="col" style="width: 225px; background: #6EB4EB">CHCS IP
</th>
</tr>
<tr style="text-align:center;">
<td>###.##.##.###</td>
<td>###.##.##.###:#####</td>
<td>###.##.##.##
</td>
</tr>
public void LoadCdrs()
{
try
{
WebClient webClient = new WebClient();
/** refreshToken is bogus, but it prevents caching of the data so we always get the latest **/
string html = webClient.DownloadString(environmentsUrl + "?refreshToken=" + Guid.NewGuid().ToString());
HtmlParser htmlParser = new HtmlParser();
IDocument document = htmlParser.Parse(html);
IElement toc = document.GetElementById("toc");
IHtmlCollection<IElement> tocLinks = toc.QuerySelectorAll("li");
foreach (IElement element in tocLinks)
{
IElement anchor = element.FirstElementChild;
Cdr environmentInfo = new Cdr(anchor, null, null);
Add(environmentInfo);
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
public Cdr(IElement anchor, Object lcs, Object snarePort) : base()
{
try
{
Name = anchor.Children[1].TextContent;
string[] hrefParts = anchor.GetAttribute("href").Split('#');
string href = hrefParts[hrefParts.Length - 1];
var element = anchor.Owner.GetElementById(href);
while (!element.TagName.Equals("table", StringComparison.CurrentCultureIgnoreCase))
{
if (element.NextSibling != null)
{
element = element.ParentElement.NextElementSibling;
}
else
{
element = element.NextElementSibling;
}
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}