我试图将从HTML文件提取的链接添加到CheckBoxList
(cbl_items
)。
到目前为止,它仍然有效,但是该项目的名称显示为HtmlAgilityPack.HtmlNode,而不是链接。我尝试使用DocumentElement
代替,Node
但它说它不存在或类似。
如何获取显示的URL而不是HtmlAgilityPack.HtmlNode?
到目前为止,这是我尝试过的:
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);
}
您是将HtmlNode
对象添加到,CheckBoxList
而不是href
属性值。您所看到的是HtmlNode
的ToString()
值,因为那是CheckBoxList
显示该对象所能做的最好的事情。
相反,您可以使用GetAttributeValue(string attribute, string defaultValue)
来检索href
属性的值。
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);
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句