2014-02-26 62 views
0

運行一小段代碼說輸入字符串不在正確的合成文件中時,出現錯誤消息。這個問題是從分析這些HTML甲酸異常拋出「輸入字符串的格式不正確」

標記時來了一個

<td class="tdRow1Color" width="100%"> 
<table width="100%" cellpadding="0" cellspacing="0" border="0"> 
    <tr><td class="plaintextbold">Item Number:&nbsp;1258</td></tr> 
     <tr><td><img alt="" src="images/clear.gif" width="1" height="10" border="0"></td></tr> 
    <tr> 
     <td class="plaintext" valign="middle">&nbsp;<img src="../images/0note.gif" border="0" align="absmiddle">&nbsp;<a class="prodlink" href="writeReview.asp?number=1258"><i><u>Be the first to review this item</u></i></a></td> 
      </tr> 
       <tr><td><img alt="" src="images/clear.gif" width="1" height="10" border="0"></td></tr>      
     <tr><td class="plaintext"><b>RRP £50.00 - Now £39.99</b>   </td> 

馬克兩個

<tr><td class="tdRow1Color" width="100%"> 
    <table width="100%" cellpadding="0" cellspacing="0" border="0">   
     <tr><td class="plaintextbold">Item Number:&nbsp;2525</td></tr> 
      <tr><td><img alt="" src="images/clear.gif" width="1" height="10" border="0"></td></tr> 
       <tr> 
       <td class="plaintext" valign="middle">&nbsp;<img src="../images/0note.gif" border="0" align="absmiddle">&nbsp;<a class="prodlink" href="writeReview.asp?number=2525"><i><u>Be the first to review this item</u></i></a></td> 
       </tr> 
       <tr><td><img alt="" src="images/clear.gif" width="1" height="10" border="0"></td></tr> 
       <tr><td class="plaintext">RRP £45 - Now £38 

我通過這個正則表達式coverting的RRP價格。

private Regex _originalPriceRegex = new Regex(@"RRP \s(\d+\.?\d+?)"); 

並通過XPath的

ProductProperties.priceOriginal, new HtmlElementLocator("//td[@class='tdRow1Color']//td[@class='plaintext']//text()[starts-with(., 'RRP')]", 

這個問題似乎是在XPath的值傳遞到下面的函數的到達拿起RRP價格。當它返回priceMatch.Groups [1]時拋出異常。值

private string LookForOrignalPrice(HtmlNode node) 
     { 

      string text = node.InnerText; 
      Match priceMatch = _originalPriceRegex.Match(text); 
      if (priceMatch.Success) 
        Console.WriteLine("++++++price is " + priceMatch); 
        return priceMatch.Groups[1].Value; 

        return null; 
     } 

感謝您提供任何建議。

+0

使用的[HTML解析器(http://htmlagilitypack.codeplex.com/)來解析HTML。另請閱讀:http://stackoverflow.com/a/1732454/932418 –

回答

4

使用時,如果它的好習慣戴上牙套,否則你可能會陷入這樣的錯誤。 這裏priceMatch.Groups[1].ValuepriceMatch.Success爲false時執行。

所以,你應該改變你的代碼是這樣的:

private string LookForOrignalPrice(HtmlNode node) 
    { 
    string text = node.InnerText; 
    Match priceMatch = _originalPriceRegex.Match(text); 
    if (priceMatch.Success) --> put braces to group the statements 
    { 
     Console.WriteLine("++++++price is " + priceMatch); 
     return priceMatch.Groups[1].Value; 
    } 
    return null; 
    } 
+0

+1,這是你不應該使用沒有大括號的'if'語句的唯一原因。 – Arran