Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

HtmlAgilityPack - How to get the tag by Id?

I have a task to do. I need to retrieve the a tag or href of a specific id (the id is based from the user input). Example I have a html like this

<manifest>  <item href="Text/Cover.xhtml" id="Cov" media-type="application/xhtml+xml" />     <item href="Text/Back.xhtml" id="Back" media-type="application/xhtml+xml" />   </manifest> 

I already have this code. Please, help me. Thank you

HtmlAgilityPack.HtmlDocument document2 = new   HtmlAgilityPack.HtmlDocument(); document2.Load(@"C:\try.html"); HtmlNode[] nodes = document2.DocumentNode.SelectNodes("//manifest").ToArray();  foreach (HtmlNode item in nodes) {     Console.WriteLine(item.InnerHtml); } 
like image 957
knowme Avatar asked May 10 '16 05:05

knowme


1 Answers

If I understand correctly then:

HtmlAgilityPack.HtmlDocument document2 = new HtmlAgilityPack.HtmlDocument(); document2.Load(@"C:\try.html");  string tag = document2.GetElementbyId("yourid").Name; string href = document2.GetElementbyId("yourid").GetAttributeValue("href", ""); 
like image 124
b00sted 'snail' Avatar answered Sep 28 '22 06:09

b00sted 'snail'