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

1 Answer 1

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']"));
Sign up to request clarification or add additional context in comments.

6 Comments

good suggestion, can this be done with using just regular regex, without any external library? =)
Yes, it can be done using regular regex. You can read more about it here: stackoverflow.com/questions/1732348/…. And once you have read this answer you can get back to HTML Agility Pack or a similar HTML parsing tool.
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 ?
Darin hahahh that was a good one :D ... Okay I understand it now better =D
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.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.