1

I'm trying to download an entire HTML page like following:

var html = new WebClient().DownloadString("http://mypage.com/"); 

and this HTML document contains a class like this:

<span class="mem_loc">United States</span>

Like this literally...

I need to find somehow now this class mem_loc and it's value , which is United states or any other country...

Is there any "easy" way that this could be done in C# ?

P.S. The structure of the Tag is always like this , so I can probably search it through the string or somehow?

P.S. I want to fetch only whats between > < values, which is a country name...

squirl
  • 1,636
  • 1
  • 16
  • 30
User987
  • 3,663
  • 15
  • 54
  • 115

1 Answers1

3

One way to achieve this is to use an HTML parser. For example HTML agility pack is one such tool. It allows you to do this:

var result = doc.DocumentNode.SelectNodes("//span[@class='mem_loc']"));
Darin Dimitrov
  • 1,023,142
  • 271
  • 3,287
  • 2,928
  • good suggestion, can this be done with using just regular regex, without any external library? =) – User987 Jan 22 '17 at 14:51
  • 1
    Yes, it can be done using regular regex. You can read more about it here: http://stackoverflow.com/questions/1732348/regex-match-open-tags-except-xhtml-self-contained-tags/1732454. And once you have read this answer you can get back to HTML Agility Pack or a similar HTML parsing tool. – Darin Dimitrov Jan 22 '17 at 14:52
  • Darin cool ty, what'cha think which way is better? I mean adding an entire new library just because of 1 line of code isn't a very good idea no ? – User987 Jan 22 '17 at 14:53
  • Darin hahahh that was a good one :D ... Okay I understand it now better =D – User987 Jan 22 '17 at 14:54
  • 3
    There's a difference between *1 line of code*, and *code that works in production*. So it will all depend on your scenario. If you want to be mocking something real quick for a presentation or a dummy website then of course you can use a regex. But if you want to write code that works in a production system then believe me, you don't want to be using regex for parsing HTML. – Darin Dimitrov Jan 22 '17 at 14:55
  • Darin, yes sir understood it clearly! Your reputation amount tells me quite a lot about you ,and you sir deserve a big respect! Thanks for all the advices =) – User987 Jan 22 '17 at 14:57