2014-09-26 57 views
0

我試圖創建一個需要網頁內容的wp 8.1應用程序。我的問題是xpath似乎不適用於WP8.1,所以我試圖使用LinQ,但我不太瞭解它。 該網頁是這樣的:使用HAP LinQ解析網頁

<body> 
    <table cellspacing="0" cellpadding="0" border="0" style="border-style:none; padding:0; margin:0;" id="ctl00_ContentPlaceHolder1_ListView1_groupPlaceholderContainer">    
     <tbody> 
      <tr style="border-style:none;padding:0; margin:0; background-image:none; vertical-align:top;" id="ctl00_ContentPlaceHolder1_ListView1_ctrl0_itemPlaceholderContainer">   
       <td style="border-style:none;padding:0; margin:0; width:22%;" id="ctl00_ContentPlaceHolder1_ListView1_ctrl0_ctl01_Td3"> 
        <div class="photo"> 
         <a target="_self" title="PH1" href="fumetto.aspx?Fumetto=279277">PH1_1</a> 
        </div> 
       </td> 
      </tr> 
      <tr style="border-style:none;padding:0; margin:0; background-image:none; vertical-align:top;" id="ctl00_ContentPlaceHolder1_ListView1_ctrl0_itemPlaceholderContainer">   
       <td style="border-style:none;padding:0; margin:0; width:22%;" id="ctl00_ContentPlaceHolder1_ListView1_ctrl0_ctl01_Td3"> 
        <div class="photo"> 
         <a target="_self" title="PH2" href="fumetto.aspx?Fumetto=279277">PH2_1</a> 
        </div> 
       </td> 
      </tr> 
      <tr style="border-style:none;padding:0; margin:0; background-image:none; vertical-align:top;" id="ctl00_ContentPlaceHolder1_ListView1_ctrl0_itemPlaceholderContainer">   
       <td style="border-style:none;padding:0; margin:0; width:22%;" id="ctl00_ContentPlaceHolder1_ListView1_ctrl0_ctl01_Td3"> 
        <div class="photo"> 
         <a target="_self" title="PH3" href="fumetto.aspx?Fumetto=279277">PH3_1</a> 
        </div> 
       </td> 
      </tr> 
     </tbody> 
    </table> 
</body> 

我要保存屬性 「PH1」, 「PH2」, 「PH3」 和值 「PH1_1」, 「PH2_1」, 「PH3_1」。你可以幫我嗎?我的代碼是這樣的:

string filePath = "..."; 
HtmlAgilityPack.HtmlDocument htmlDoc = new HtmlAgilityPack.HtmlDocument(); 
htmlDoc.OptionFixNestedTags = true; 
htmlDoc.LoadHtml(filePath); 
if (htmlDoc.ParseErrors != null && htmlDoc.ParseErrors.Count() > 0) 
{ 
    // Handle any parse errors as required 
} 
else 
{ 
    if (htmlDoc.DocumentNode != null) 
    { 
     //I'm trying to get the first node for now 
     HtmlAgilityPack.HtmlNode aNode = htmlDoc.DocumentNode.DescendantsAndSelf("a").FirstOrDefault(); 
     if (aNode != null) 
     { 
      string first = aNode.GetAttributeValue("title", "null"); 
      string value = aNode.ToString(); 
      ... 
     } 
    } 
} 
+0

你現在的代碼有什麼問題? – har07 2014-09-26 13:45:26

+0

問題是'first'是'「null」,'value'是''htmlAgilitypack'''。 – 2014-09-26 13:47:57

回答

1

嘗試與Descendants()取代DescendantsAndSelf()

HtmlAgilityPack.HtmlNode aNode = htmlDoc.DocumentNode 
             .Descendants("a") 
             .FirstOrDefault(); 

而不是調用ToString(),用InnerText屬性來獲取開cloaing標籤之間的文本:

if (aNode != null) 
{ 
    string first = aNode.GetAttributeValue("title", "null"); 
    string value = aNode.InnerText; 
    ..... 
} 

[.NET fiddle demo]

+0

'aNode'現在是'null' ..如果我保留'DescendantsAndSelf()',我會爲'first'和'value'頁面的鏈接取'null'。 – 2014-09-26 13:53:43

+0

不確定'DescendantsAndSelf()',它有點爲我返回錯誤的元素(也許是HAP中的一個錯誤,沒有進一步檢查)。但'後裔()'應該工作,看演示[在dotnetfiddle](https://dotnetfiddle.net/61fc10) – har07 2014-09-26 14:00:21

+0

另一個問題。我如何獲得html節點,如果我有這樣的事情?我總是得到第一個節點,而不是第二個節點。 '<!DOCTYPE HTML> ...' – 2014-09-26 16:28:00