2012-03-07 66 views
0
<td valign="top" class="m92_h_bigimg"> 
    <a href="#pagetop" onclick="drop1('hotelinfos');" title="Hotelinformationen einblenden"><img border=0 src="http://i2.giatamedia.de/s.php?uid=168846&source=xml&size=320&vea=5vf&cid=2492&file=007399_8790757.jpg" name="bigpic"></a> 
</td> 
<td valign="top" class="m92_h_bigimg2"> 
    <table border=0 cellpadding=0 cellspacing=0> 
     <tr> 
      <td valign="top" class="m92_h_para">Hotel:</td> 
      <td valign="top" class="m92_h_name"> 
       Melia Tropical    <br> 
       <img src="/images/star.gif" height=13 width=13 alt="*"><img src="/images/star.gif" height=13 width=13 alt="*"><img src="/images/star.gif" height=13 width=13 alt="*"><img src="/images/star.gif" height=13 width=13 alt="*"><img src="/images/star.gif" height=13 width=13 alt="*">    
      </td> 
     </tr> 
     <tr> 
      <td valign="top" class="m92_h_para">Zimmer:</td> 
      <td valign="top" class="m92_h_wert"><b>Suite</b></td> 
     </tr> 
     <tr> 
      <td valign="top" class="m92_h_para">Verpflegung:</td> 
      <td valign="top" class="m92_h_wert"><b>All Inclusive</b></td> 
     </tr> 
     <tr> 
      <td valign="top" class="m92_h_para">Ort:</td> 
      <td valign="top" class="m92_h_wert">Punta Cana</td> 
     </tr> 
     <tr> 
      <td valign="top" class="m92_h_para">Region:</td> 
      <td valign="top" class="m92_h_wert">Punta Cana</td> 
     </tr> 
     <tr> 
      <td valign="top" class="m92_h_para">Land:</td> 
      <td valign="top" class="m92_h_wert">Dom. Republik</td> 
     </tr> 
     <tr> 
      <td valign="top" class="m92_h_para">Anbieter:</td> 
      <td valign="top" class="m92_h_wert"><a href="javascript:VA('5VF');"><img border=0 src="http://www.lmweb.net/lmi/va/gifs/5VF.gif" alt="5 vor Flug" title="5 vor Flug"></a><br>5 vor Flug</td> 
     </tr> 
    </table> 
    <table border=0 cellpadding=0 cellspacing=0> 
     <tr> 
      <td><img src="/images/dropleftw.gif" height="16" width="18"></td> 
      <td> 
       <div id="mark" class="m92_notice"> 

         &nbsp;<a target="vakanz" href="siteplus/reminder.php?session_id=rslr1ejntpmj07n0f2smqfhsj5&REC=147203&m_flag=1&m_typ=hotel">Dieses Hotel merken</a> 
     </div> 
      </td> 
     </tr> 
     <tr> 
      <td><img src="/images/dropleftw.gif" height="16" width="18"></td> 
      <td> 
      <div class="m92_notice"> 
      &nbsp;<a href="#pagetop" onclick="drop1('hotelinfos'); drop1('hoteltermine');">Hotelbewertung anzeigen</a> 
      </div> 
      </td> 
     </tr> 
    </table> 
</td> 

隨着HtmlAgility包,我怎麼能得到<td valign="top" class="m92_h_bigimg">和他的收盤<td>之間的數據。我試着用這個代碼不使用HtmlAgility-pack,這個工程,但它發現第一個</td>並關閉。所以代碼是不正確的。我讀過HtmlAgility-pack是這類問題的最佳解決方案。讀取數據開始到結束

public static string[] GetStringInBetween(string strBegin, string strEnd, string strSource, bool includeBegin, bool includeEnd) 
{ 
    string[] result = { "", "" }; 
    int iIndexOfBegin = strSource.IndexOf(strBegin, StringComparison.Ordinal); 

    if (iIndexOfBegin != -1) 
    { 
     int iEnd = strSource.IndexOf(strEnd, iIndexOfBegin, StringComparison.Ordinal); 

     if (iEnd != -1) 
     { 
      result[0] = strSource.Substring(iIndexOfBegin + (includeBegin ? 0 : strBegin.Length), iEnd + (includeEnd ? strEnd.Length : 0) - iIndexOfBegin); 

      if (iEnd + strEnd.Length < strSource.Length) 
         result[1] = strSource.Substring(iEnd + strEnd.Length); 
     } 
    } 
    return result; 
} 

我該怎麼做?

+0

例如對於這個特定的搜索正則表達式可能是一個更簡單的解決方案,在AgilityPack中使用SelectNodes(「...」);-) – jwillmer 2012-03-07 09:45:07

回答

1
HtmlAgilityPack.HtmlDocument htmlDoc = new HtmlAgilityPack.HtmlDocument(); 
htmlDoc.LoadHtml(html); 
var str = htmlDoc.DocumentNode 
    .Descendants("td") 
    .Where(x => x.Attributes["class"] != null && x.Attributes["class"].Value == "m92_h_bigimg") 
    .Select(x => x.InnerHtml) 
    .First(); 
+0

thx你很LB – senzacionale 2012-03-07 12:44:03

1

的HtmlAgilityPack支持標準XPath查詢,所以我認爲你可以這樣做:

foreach (var node in doc.DocumentElement.SelectNodes("//td[@class='m92_h_bigimg']")) 
{ 
    // Do work on your node. 
} 

...其中doc是你的HtmlDocument