FronTBlazze68
FronTBlazze68

Reputation: 13

How can I convert an HtmlAttribute to a string?

var web = new HtmlWeb();
var doc = web.Load(page);

var Articles = doc.DocumentNode.SelectNodes("//*[@class = 'b-product-grid-tile js-tile-container']"); 
var href = doc.DocumentNode.SelectNodes("//a[@href]");

foreach (HtmlNode link in href)
            {
                HtmlAttribute att = link.Attributes["href"];
                _entries.Add(new EntryModel { Link = att });

                // att.ToString(); <----- Want to convert the HtmlAttribute to a string.
            }

Full Code of my Scraper:

Full Code of my Scraper

EntryModel List:

EntryModel List

Main Window:

Main Window

The links I need:

The links I need

Upvotes: 1

Views: 63

Answers (1)

H.S
H.S

Reputation: 190

The easiest would be using the System.Web.Mvc.TagBuilder class:

var attributes = new { @class = "myClass", id = "elId" };

var tag = new TagBuilder("href");
tag.MergeAttributes(new RouteValueDictionary(attributes));

return tag.ToString();

Upvotes: 1

Related Questions