How can I get values from Html Tags?

I want to get some data from html tags in a web page. For example I have a web site that’s got “www.example.com/test.html”, this is text which I want to split. I want to first URL, First text thats between in first span tags and last text that’s between the last span tags.

How can I do this with C# ASP.NET 2.0 (not 3.5)?

<a class="tablolinkmetin" target="_blank" href="http://www.iwantthisurl.com/test/2010/subat/12022010_adli_krrnme.htm" rel="nofollow noreferrer noopener" rel="nofollow noreferrer noopener">
  <img alt=icon src="images/icon/ok.gif" border=0 width="7" height="8">
  <span class=tablolink>
    <span class="genelgeler_mbaslik">I want this text.</span>
  </span>
  <span class="tablolinkaltyazi"><br>and i want here</span> 
</a>
<img src="images/icon/cizgi.png" width="260" height="1"><br>

Here is Solutions:

We have many solutions to this problem, But we recommend you to use the first solution because it is tested & true solution that will 100% work for you.

Solution 1

you need to have a look at:


here is the sample from codePlex.com

 HtmlDocument doc = new HtmlDocument();
 doc.Load("file.htm");
 foreach(HtmlNode link in doc.DocumentElement.SelectNodes("//a[@href"])
 {
    HtmlAttribute att = link["href"];
    att.Value = FixLink(att);
 }
 doc.Save("file.htm");

Hope this helps

Solution 2

If you give the controls an id and set them to runat=”server” you should be able to reference them directly in your code.

So your page should look like this:

<a id="myanchor" runat="server" class="tablolinkmetin" target="_blank" href="http://www.iwantthisurl.com/test/2010/subat/12022010_adli_krrnme.htm" rel="nofollow noreferrer noopener" rel="nofollow noreferrer noopener"> 
  <img alt=icon src="images/icon/ok.gif" border=0 width="7" height="8"> 
  <span class=tablolink> 
    <span id="firstSpan" runat="server" class="genelgeler_mbaslik">I want this text.</span> 
  </span> 
  <span id="secondSpan" runat="server" class="tablolinkaltyazi"><br>and i want here</span>  
</a> 
<img src="images/icon/cizgi.png" width="260" height="1"><br> 

Note: Use and implement solution 1 because this method fully tested our system.
Thank you 🙂

All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply