Selecting attribute values with html Agility Pack

扶醉桌前 提交于 2019-11-26 23:01:06

Html Agility Pack does not support attribute selection.

Pierluc SS

You can directly grab the attribute if you use the HtmlNavigator instead.

//Load document from some html string
HtmlDocument hdoc = new HtmlDocument();
hdoc.LoadHtml(htmlContent);

//Load navigator for current document
HtmlNodeNavigator navigator = (HtmlNodeNavigator)hdoc.CreateNavigator();

//Get value from given xpath
string xpath = "//div[@id='topslot']/a/img/@src";
string val = navigator.SelectSingleNode(xpath).Value;

You may use the method "GetAttributeValue".

Example:

//[...] code before needs to load a html document
HtmlAgilityPack.HtmlDocument htmldoc = e.Document;
//get all nodes "a" matching the XPath expression
HtmlNodeCollection AllNodes = htmldoc.DocumentNode.SelectNodes("*[@class='item']/p/a");
//show a messagebox for each node found that shows the content of attribute "href"
foreach (var MensaNode in AllNodes)
{
     string url = MensaNode.GetAttributeValue("href", "not found");
     MessageBox.Show(url);
}

Reading and Writing Attributes with Html Agility Pack

You can both read and set the attributes in HtmlAgilityPack. This example selects the < html> tag and selects the 'lang' (language) attribute if it exists and then reads and writes to the 'lang' attribute.

In the example below, the doc.LoadHtml(this.All), "this.All" is a string representation of a html document.

Read and write:

            HtmlAgilityPack.HtmlDocument doc = new HtmlAgilityPack.HtmlDocument();
            doc.LoadHtml(this.All);
            string language = string.Empty;
            var nodes = doc.DocumentNode.SelectNodes("//html");
            for (int i = 0; i < nodes.Count; i++)
            {
                if (nodes[i] != null && nodes[i].Attributes.Count > 0 && nodes[i].Attributes.Contains("lang"))
                {
                    language = nodes[i].Attributes["lang"].Value; //Get attribute
                    nodes[i].Attributes["lang"].Value = "en-US"; //Set attribute
                }
            }

Read only:

            HtmlAgilityPack.HtmlDocument doc = new HtmlAgilityPack.HtmlDocument();
            doc.LoadHtml(this.All);
            string language = string.Empty;
            var nodes = doc.DocumentNode.SelectNodes("//html");
            foreach (HtmlNode a in nodes)
            {
                if (a != null && a.Attributes.Count > 0 && a.Attributes.Contains("lang"))
                {
                    language = a.Attributes["lang"].Value;
                }
            }

I used the following way to obtain the attributes of an image.

var MainImageString  = MainImageNode.Attributes.Where(i=> i.Name=="src").FirstOrDefault();

You can specify the attribute name to get its value; if you don't know the attribute name, give a breakpoint after you have fetched the node and see its attributes by hovering over it.

Hope I helped.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!