Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I am trying to add links extracted from an HTML file to a CheckBoxList (cbl_items).

It works so far but instead of the link, the item's name is displayed as HtmlAgilityPack.HtmlNode. I tried using DocumentElement instead of Node but it said that it does not exist or similar.

How can I get the URL to be displayed instead of HtmlAgilityPack.HtmlNode?

This is what I've tried so far:

HtmlWeb hw = new HtmlWeb();
HtmlAgilityPack.HtmlDocument doc = new HtmlAgilityPack.HtmlDocument();
doc = hw.Load(tb_url.Text);
foreach (HtmlNode link in doc.DocumentNode.SelectNodes("//a[@href]"))
{
  cbl_items.Items.Add(link);
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
844 views
Welcome To Ask or Share your Answers For Others

1 Answer

You are adding the HtmlNode object to the CheckBoxList and not the value of the href attribute. What you are seeing is the HtmlNode's ToString() value since that's the best that the CheckBoxList can do to display that object.

Instead, you can use GetAttributeValue(string attribute, string defaultValue) to retrieve the href attribute's value.

HtmlWeb hw = new HtmlWeb();
HtmlAgilityPack.HtmlDocument doc = new HtmlAgilityPack.HtmlDocument();
doc = hw.Load(tb_url.Text);
foreach (HtmlNode link in doc.DocumentNode.SelectNodes("//a[@href]"))
{
    // Get the value of the HREF attribute
    string hrefValue = link.GetAttributeValue( "href", string.Empty );
    cbl_items.Items.Add(hrefValue);
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...