0

I am getting an error message when running a small bit of code saying that the input string was not in a correct formate. The issue is coming from when parsing these html

Mark up One

<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>

Mark up Two

<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   

I am coverting the RRP price through this regex.

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

And picking up the RRP prices through the xpath

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

The issue seems to be arrive when the xpath value is passed into the functio below. The exception is being thrown when it returns priceMatch.Groups[1].Value

 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;
        }

Thanks for any advice which you can give.

willa
  • 629
  • 1
  • 9
  • 17
  • Use an [html parser](http://htmlagilitypack.codeplex.com/) to parse Html. Also read this: http://stackoverflow.com/a/1732454/932418 – L.B Feb 26 '14 at 10:36

1 Answers1

4

When using if its good practice to put braces, or else you might fall to errors like this. Here priceMatch.Groups[1].Value is executed when priceMatch.Success is false.

So you should change your code like this:

  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;
  }
ssilas777
  • 9,672
  • 4
  • 45
  • 68
  • +1, this is the sole reason you shouldn't ever use the `if` statement without curly braces. – Arran Feb 26 '14 at 10:33