Click here to Skip to main content
15,944,136 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
how to parse links (link1.php, link2.php...) from the source shown below add them for example in a listbox?
i want to parse links only where the "span class="visitext">VISIT"
i tried with functions GetElementsByTagName, GetElementById and GetAttribute but simply do not know how to add the condition "VISIT"

<a href="link1.php" target="_blank"> VISIT</a>
<a href="link2.php" target="_blank"> VISITED</a>
<a href="link3.php" target="_blank"> VISITED</a>
<a href="link4.php" target="_blank"> VISIT</a>

If you know the document is well-formed XHTML you can use XPath and/or LINQ. Sorry, I don't know VB, but hopefully this is self-explanatory.

using System;
using System.Xml;
using System.Xml.Linq;
using System.Xml.XPath;


var xhtml = "put your xhtml here";
var xml = XDocument.Parse(xhtml);
var visitNodes = xml.XPathSelectElements("//span[@class='visit']/a/@href");
var links = from n in visitNodes select n.Value;

foreach(var l in links)

If you're stuck with .NET 2.0 or older, you can do the same thing with a few more lines of code using XmlDocument instead of XDocument.

If the HTML isn't well-formed, you'll need to use regular expressions. Here's a good starting point.[^]
Share this answer
Well, it seems to me like simple string mashing will work, given that the link is always in the same place, but I'd say reguarly expressions are the most robust way to do it.
Share this answer

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900