Skip to content Skip to sidebar Skip to footer

Selecting Attribute Values With Html Agility Pack

I'm trying to retrieve a specific image from a html document, using html agility pack and this xpath: //div[@id='topslot']/a/img/@src As far as I can see, it finds the src-attribu

Solution 1:

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

//Load document from some html stringHtmlDocumenthdoc=newHtmlDocument();
hdoc.LoadHtml(htmlContent);

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

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

Solution 2:

Html Agility Pack does not support attribute selection.

Solution 3:

You may use the method "GetAttributeValue".

Example:

//[...] code before needs to load a html document
HtmlAgilityPack.HtmlDocumenthtmldoc= e.Document;
//get all nodes "a" matching the XPath expressionHtmlNodeCollectionAllNodes= 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)
{
     stringurl= MensaNode.GetAttributeValue("href", "not found");
     MessageBox.Show(url);
}

Solution 4:

Solution 5:

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;
                }
            }

Post a Comment for "Selecting Attribute Values With Html Agility Pack"