C# 代码只首先解析<;部门></部门>;小孩

C# 代码只首先解析<;部门></部门>;小孩,c#,html-agility-pack,C#,Html Agility Pack,我在主节点中有3个子节点。但是我的代码只解析第一个子项(即标题,即日期、时间等)。调试时,第一轮运行正常(cols到cols8似乎有值),但当(int i=0;i

我在主
节点中有3个子节点
。但是我的代码只解析第一个子项(即标题,即日期、时间等)。调试时,第一轮运行正常(cols到cols8似乎有值),但当(int i=0;i的代码在第二轮运行时,cols到cols8变为
null
。任何帮助都将不胜感激!谢谢大家!


日期
时间
贸易中国
体积
买卖
投标
问
价值
2014年1月8日
17:05:00
83.80
1,970,861
出售*
83.85
83.88
1.652M
2014年1月7日
16:30:11
80.739
40,000
出售*
83.85
83.88
32.30k
我的代码:

// 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();
 }
//加载HTML
HtmlAgilityPack.HtmlDocument doc=新的HtmlAgilityPack.HtmlDocument();
doc.Load(文件名);
//获取文档中的特定节点
HtmlNode node=doc.DocumentNode.SelectSingleNode(//div[@class='RecentTrades']);
使用(FileStream fs=newfilestream(@.\..\bin\Debug\modifiedpage\“+”RecentShareTrades.txt“,FileMode.Append))
使用(StreamWriter sw=新StreamWriter(fs))
{
//迭代相关div中的所有行
HtmlNodeCollection rows=node.SelectNodes(“.//div”);
对于(int i=0;i
您可能需要阅读一些XPath简介(甚至是规范的第一部分-。。。我不知道你为什么在任何地方都使用
/
(所有节点匹配标准)-可能会选择比你需要的更多的节点(并且比显式路径(如
)“/div/div/dvi[@class='tradessk']”有效得多。
嗨,Alexei,我尝试过使用这种方法
”/div/div/div[@class='tradesk']”
但它也不起作用。我一直在玩
/
但没有希望。路径只是一个人的样子的随机样本,很抱歉样本太真实。显式路径更快,以后更容易理解(因为你不会从整个树中抓取节点)是的,我知道这只是一个示例,谢谢您的帮助,不必道歉。:)只是一个问题,
,它们都有
class=TradesRow
。我将如何编写这一行
HtmlNodeCollection rows=node.SelectNodes(“.//div[@class='TradesRow']”)
如果我想让它只检测第一个
,即
class=“TradesRow
?如果您不回答,那没关系。谢谢。
// 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();
 }