我在主<div></div>
节点中有3个孩子<div class="RecentTrades"></div>
。但我的代码只解析了第一个孩子(即标题,即日期,时间等)。在调试时,第一轮是好的(cols到cols8似乎有值),但是当代码for (int i = 0; i < rows.Count; ++i)
运行第二轮时,cols到cols8变为null
。任何帮助将非常感激!谢谢!
<div></div>
:
<div class="RecentTrades">
<div class="TradesRow TradesHeader">
<div class="TradesDate">Date</div>
<div class="TradesTime">Time</div>
<div class="TradesPrice">Trade Prc</div>
<div class="TradesVolume">Volume</div>
<div class="TradesBuySell">Buy/Sell</div>
<div class="TradesBid">Bid</div>
<div class="TradesAsk">Ask</div>
<div class="TradesValue">Value</div>
</div>
<div class="TradesRow TradesBG_G" style="color:#ff0000;">
<div class="TradesDate">08-Jan-14</div>
<div class="TradesTime">17:05:00</div>
<div class="TradesPrice">83.80</div>
<div class="TradesVolume">1,970,861</div>
<div class="TradesBuySell">Sell* </div>
<div class="TradesBid">83.85</div>
<div class="TradesAsk">83.88</div>
<div class="TradesValue">1.652<strong>M</strong></div>
</div>
<div class="TradesRow TradesBG_W" style="color:#ff0000;">
<div class="TradesDate">07-Jan-14</div>
<div class="TradesTime">16:30:11</div>
<div class="TradesPrice">80.739</div>
<div class="TradesVolume">40,000</div>
<div class="TradesBuySell">Sell* </div>
<div class="TradesBid">83.85</div>
<div class="TradesAsk">83.88</div>
<div class="TradesValue">32.30<strong>k</strong></div>
</div>
</div>
我的代码:
// Load HTML
HtmlAgilityPack.HtmlDocument doc = new HtmlAgilityPack.HtmlDocument();
doc.Load(fileName);
// Get specific node in the document
HtmlNode node = doc.DocumentNode.SelectSingleNode("//div[@class='RecentTrades']");
using (FileStream fs = new FileStream(@"..\..\bin\Debug\ModifiedPages\" + "RecentShareTrades.txt", FileMode.Append))
using (StreamWriter sw = new StreamWriter(fs))
{
// Iterate all rows in the relevant div
HtmlNodeCollection rows = node.SelectNodes(".//div");
for (int i = 0; i < rows.Count; ++i)
{
// Iterate all columns in this row
HtmlNodeCollection cols = rows[i].SelectNodes(".//div[@class='TradesDate']");
HtmlNodeCollection cols2 = rows[i].SelectNodes(".//div[@class='TradesTime']");
HtmlNodeCollection cols3 = rows[i].SelectNodes(".//div[@class='TradesPrice']");
HtmlNodeCollection cols4 = rows[i].SelectNodes(".//div[@class='TradesVolume']");
HtmlNodeCollection cols5 = rows[i].SelectNodes(".//div[@class='TradesBuySell']");
HtmlNodeCollection cols6 = rows[i].SelectNodes(".//div[@class='TradesBid']");
HtmlNodeCollection cols7 = rows[i].SelectNodes(".//div[@class='TradesAsk']");
HtmlNodeCollection cols8 = rows[i].SelectNodes(".//div[@class='TradesValue']");
for (int j = 0; j < cols.Count; ++j)
// Get the value of the column and print it
sw.WriteLine(cols[j].InnerText + "," + cols2[j].InnerText + "," + cols3[j].InnerText + "," + cols4[j].InnerText.Replace(",", "") + "," + cols5[j].InnerText + "," + cols6[j].InnerText + "," + cols7[j].InnerText + "," + cols8[j].InnerText + "," + row.Cells[0].Value.ToString());
}
sw.Flush();
sw.Close();
fs.Close();
}