-1

I'm developing software that grabs data from an html table. So this line:

team.SelectSingleNode(".//td[@class='number total won total_won']")?.InnerText.Trim();

returns: ""

(I'm using html agility pack for DOM manipulation.)

And the complete line is this:

Convert.ToInt32(
  team.SelectSingleNode(
    ".//td[@class='number total won total_won']")
  ?.InnerText.Trim());

This returns an exception (incorrect format exception).

Any idea to solve this?

Sean Newell
  • 1,033
  • 1
  • 10
  • 25
Ilnumerouno
  • 65
  • 1
  • 6
  • What do you want the value to be if it is a n empty string? Indeed an empty string is not a valid int – Gilad Green Oct 13 '17 at 14:47
  • @GiladGreen, The value should be 0 if there is no value – Ilnumerouno Oct 13 '17 at 14:47
  • 3
    seems like you can solve this with a straight forward "if" statement... – sous2817 Oct 13 '17 at 14:48
  • `var str = team.SelectSingleNode(".//td[@class='number total won total_won']")?.InnerText.Trim();` `var number = str.IsNullOrWhiteSpace ? 0 : Convert.ToInt32(str);` is one way to go, if you want zero value for null, empty or whitespace only string representation. – Sergey.quixoticaxis.Ivanov Oct 13 '17 at 14:49
  • Personally I'd just check if it is an empty string and if so return 0. You could also use TryParse and if it failed to parse the content as an int set it to 0 but in this case you'd mask an error if the table ended up containing other unexpected data (eg "eight" or "John" or something else that you probably actually want to know about). – Chris Oct 13 '17 at 14:49
  • I've a lot of line of code to convert, I need to add a lot of condition per line, this is very bad – Ilnumerouno Oct 13 '17 at 14:50

1 Answers1

4

You can use int.TryParse instead of Convert.ToInt32

int myInt;
if(!int.TryParse(team.SelectSingleNode(".//td[@class='number total won total_won']")?.InnerText.Trim(), out myInt))
{
  myInt = 0;
}

I know, but I've 30+ lines of code, so I should add a lot of if conditions... – Ilnumerouno just now

You can write a helper method instead.

public static class Converter{
    public static int ConvertToInt(string stringAsInt){
      int myInt;
      return int.TryParse(stringAsInt, out myInt) ? myInt : 0;
    }
}

Calling code.

var parsedInt = Converter.ConvertToInt(team.SelectSingleNode(".//td[@class='number total won total_won']")?.InnerText.Trim());
Igor
  • 60,821
  • 10
  • 100
  • 175
  • I know, but I've 30+ lines of code, so I should add a lot of if conditions... – Ilnumerouno Oct 13 '17 at 14:49
  • 3
    @Ilnumerouno: Then put it in a method of its own? eg `TryGetInt(team, ".//td[@class='number total won total_won']")`. Frankly it sounds like you should have been extracting this to a method much earlier if you have that many trims, parses and inner texts as well... – Chris Oct 13 '17 at 14:51
  • @Ilnumerouno refactor your code, keep it DRY (Dont repeat yourself) – maccettura Oct 13 '17 at 14:52
  • @Ilnumerouno since this is standard copy-pasted answer you can check one of more canonical duplicates like https://stackoverflow.com/questions/9372210/int-parse-input-string-was-not-in-a-correct-format#comment62008800_9372761 that explains that `if` is optional. – Alexei Levenkov Oct 13 '17 at 14:52
  • I accept your answer, but the downvotes are a shame – Ilnumerouno Oct 16 '17 at 10:19