2015-03-19 72 views
0

我有這種格式的許多表:XPath查詢工作不適合此表

<table class="DataRows" frame="myFrames" rules="Standard" width="100%"> 

    <colgroup><col width="70" align="CENTER"> 
    <col width="200" align="LEFT"> 
    <col width="80" align="LEFT"> 
    <col align="LEFT"> 
    <col align="RIGHT"> 

    </colgroup><thead> 

    <col width="70" align="CENTER"> 
    <col width="200" align="LEFT"> 
    <col width="80" align="LEFT"> 
    <col align="LEFT"> 
    <col align="RIGHT"> 

    <thead> 

    <tr> 
    <td valign="TOP"><span class="classicBold"> 20 </span> Kg. 
    <td class="BOLD" valign="TOP" nowrap=""> 
     PA Passion Foods Inc. 
    <td class="BOLD">Fax: 
    <td> 
     222-555666 
    <td class="BOLD"> 
     Processed foods and juices 

    <tr> 
    <td><a target="_blank" href="">See on Map </a> 
    <td> 
     120 NW 157TH AVE 
    <td class="BOLD">Warehouse Hours: 
    <td colspan="2"> 


    <tr> 
    <td> 
    <td><span class="BOLD"> 
     Jacksonville, 
     </span> 
     FL 300000 
    <td class="BOLD">Url: 
    <td colspan="2"> 
     <a target="_blank" href="">PA Passion</a> 
     &nbsp&nbsp 
     <span class="BOLD">E-mail:</span> 
     [email protected] 

    <tr> 
    <td> 
    <td class="REDBOLD" colspan="4"> 


    <tr> 
    <td> 
    <td colspan="4" align="LEFT">Franchisee for:<span class="BOLD"> 
Nutrella 


</span> 
    <tr> 
    <td> 
    <td colspan="4" align="LEFT">Franchisee for:<span class="BOLD"> 
APPLE Foods, Constants 
</span> 
    <tr> 
    <td> 
    <td colspan="4" align="LEFT"><span class="BOLD"> 

</span> 

    <tr> 
    <td> 
    <td colspan="4" align="LEFT">We service:<span class="BOLD"> 
All occasions and hospitality services 
</span> 

    <tr> 
    <td> 
    <td colspan="4" align="LEFT">We sell :<span class="BOLD"> 
---- 
</span> 

</td></td></tr></td></td></tr></td></td></tr></td></td></tr></td></td></tr></td></td></tr></td></td></td></td></tr></td></td></td></td></tr></td></td></td></td></td></tr> 
    </thead> 
</table> 

我使用HtmlAgilityPack循環通每個表使用此代碼

foreach (HtmlNode node in htmlAgilityPackDoc.DocumentNode.SelectNodes("//table[contains(@class,'DataRows')]")) 
{ 

} 

這給了我每個迭代的整個節點之一就是上面的表格。我嘗試使用下面的代碼在每次迭代中訪問公司名稱。

string str= node.ChildNodes.Descendants() .SelectSingleNode("//td[@class='BOLD']").InnerText 

但我得到的是每個循環中提取的表的第一個表的公司名稱。當我通過循環中的每個表時,如何獲取下一個公司名稱和地址?

+1

''//總是從根元素開始。使用'.'作爲當前上下文,例如'「.// td [@ class ='BOLD']」' – 2015-03-19 11:13:44

回答

1

這是一個常見的錯誤,當一個試圖做一個相對XPath啓動與//軸。儘管您從node變量中調用SelectSingleNode(),但XPath仍被視爲全局變量,這意味着它相對於XML的根元素。這就是爲什麼每次總是獲取相同的元素的原因,它是整個XML中第一個匹配的元素。

爲了使電流node元素中的XPath範圍的地方,簡單地把一個點(.)在的XPath的開頭:

string str = node.SelectSingleNode(".//td[@class='BOLD']") 
       .InnerText; 
+0

謝謝。在提問時,我似乎錯過了一個重要部分。您的解決方案爲我獲取名稱。我也想要公司地址。我是否應該在單獨的問題中提出錯過的部分,或者您能否在這裏自己回答?我在這裏添加了錯過的部分。謝謝。 – user20358 2015-03-19 11:56:29

+0

如何識別地址節點?它是在第四個''之前的'​​'節點嗎?如果是這樣,你可以試試:'「(.// td [@ class ='BOLD'])[4]/preceding-sibling :: td」' – har07 2015-03-19 12:04:13

+0

如果上面的不行,我會建議發佈一個單獨的問題,並解釋你期望得到的地址給定的樣本'

',也許你會如何識別純文字中的元素... – har072015-03-19 12:08:11

0
node.SelectSingleNode(By.Xpath(.//td[@class='BOLD'])).Innertext 

這可能有效。 正如在評論中所說,使用HAP應XPath用作從前xpath「擴展」開始「 - 」 - 當前節點,如果我沒記錯

+0

By.Xpath ... can not find'By'does not need some reference? – user20358 2015-03-19 11:47:24

+0

SelectSingleNodes(「Xpath」)。InnerText應該做 – Helmer 2015-03-19 12:17:00