Click here to Skip to main content
15,886,518 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I use htmlagilitypack
I want to get all atributes href of tag [a] in content html
So, I use method SelectNodes() but it return null

This is source content html:
C#
<table class="table">
	<tr>
		<td class="frame">
			<table class="frame" id="highlight">
				<tr class="colhead">
					<td id="categ_icon">
						<a href="#" class="colhead">Category</a>
					</td>
					<td id="name">
						<a href="?sort=name&h=2&d=ASC" class="colhead">Name</a>
					</td>
					<td id="links">
						<a href="#" class="colhead">Links</a>
					</td>
					<td id="files">
						<a href="?sort=numfiles&h=3&d=DESC" class="colhead">Files</a>
					</td>
					<td id="comments">
						<a href="?sort=comments&h=4&d=DESC" class="colhead">Comm.</a>
					</td>
					<td id="added">
						<a href="?sort=added&h=6&d=DESC" class="colhead">Added</a>
					</td>
					<td id="size">
						<a href="?sort=size&h=8&d=DESC" class="colhead">Size</a>
					</td>
					<td id="snatch">
						<a href="?sort=times_completed&h=9&d=DESC" class="colhead">Snatch</a>
					</td>
					<td id="seeders">
						<a href="?sort=seeders&h=10&d=DESC" class="colhead">Seed</a>
					</td>
					<td id="leechers">
						<a href="?sort=leechers&h=11&d=DESC" class="colhead">Leech</a>
					</td>
									</tr>
				<tr class="torrent_1"><td class="table_categ_icon"><a class="link" href="?cat=545" onmouseover="return overlib('Game of Thrones');"  önmouseout="return nd();"><img src="http://static.freshon.tv/pic/catids/545.gif" width="80" height="44" alt="category_icon" /></a></td><td class="table_name"><div>
											<a <big>href="/details.php?id=108260"</big> class="torrent_name_link"title="Game.of.Thrones.S03E08.PROPER.HDTV.x264-2HD">Game.of.Thrones.S03E08.PROPER.HDTV.x264-2HD</a>
										</div>

										<div class="table_genre_nf"><a href="/browse.php?filter=drama" class="normal">Drama</a> | <a href="/browse.php?filter=family" class="normal">Family</a> | <a href="/browse.php?filter=fantasy" class="normal">Fantasy</a></div></td><td class="table_links"><a href="/download.php?id=108260&type=torrent" class="link">
									<img src="http://static.freshon.tv/pic/download_12.png" alt="DW" onmouseover="return overlib('Download torrent');"  önmouseout="return nd();" width="12" height="12"/>
								</a><a href="/bookmarks.php?type=add&tid=108260" class="link">
									<img src="http://static.freshon.tv/pic/bookmark_add_12.png" alt="BK_add" onmouseover="return overlib('Bookmark torrent');"  önmouseout="return nd();" width="12" height="12"/>
									</a></td><td class="table_files">
									<a href="/details.php?id=108260&files=1#files_tab" class="link">
										2
									</a>
							</td><td class="table_comments">
									<a href="details.php?id=108260&page=0#comments_tab" class="link">
										0
									</a>


I want to get : href="/details.php?id=108260"(I did big size font in above html)

This is my code:
C#
public string GetLinkOfNews(string xpath)
{
    string content = GoToBrowseWebsite("http://abc.com");

    string htmldoc = "";

    HtmlAgilityPack.HtmlDocument document = new HtmlAgilityPack.HtmlDocument();
    document.LoadHtml(content);

    HtmlNodeCollection nodeCollection = document.DocumentNode.SelectNodes(xpath);
    foreach (HtmlNode node in nodeCollection)
    {
       htmldoc += node.ToString();
    }

    return htmldoc;
}

C#
public string GoToBrowseWebsite(string url)
{
    HttpWebRequest requestBR = (HttpWebRequest)WebRequest.Create(url);
    requestBR.Method = "GET";
    requestBR.ContentType = "application/x-www-form-urlencoded";
    requestBR.CookieContainer = ReadCookiesFromDisk("E:\\cookie.txt");
    HttpWebResponse responseBR = (HttpWebResponse)requestBR.GetResponse();

    StreamReader reader = new StreamReader(responseBR.GetResponseStream());
    string htmldoc = reader.ReadToEnd();
    responseBR.Close();
    return htmldoc;
}


this is my xpath :
C#
/table/tr/td/div/a[@href]


somebody help me!!!
Posted
Updated 19-May-13 21:18pm
v2
Comments
Guirec 20-May-13 3:19am    
the method is probably returning null because the value of your xpath param is not a one which returns any result...
If you want to get some result you should use :

GetLinkOfNews("//a");

is this what you are doing?
sergio090588 20-May-13 3:27am    
but I want to get href of tag [a].It's atributes
sergio090588 20-May-13 4:00am    
somebody help me!!!!

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