Selecting attribute value using XPath and HtmlAgilityPack

c# html html-agility-pack xml xpath

Question

I am trying to get second attribute value of a meta tag using an xpath expression in html agility pack: The meta tag:

<meta name="pubdate" content="2012-08-30" />

The xml path expression i am using:

//meta[@name='pubdate']/@content

But it does not return anything. I tried to search around and implement this solution:

//meta[@name='pubdate']/string(@content)

Another way:

string(//meta[@name='pubdate']/@content)

But it gives xml exception in html agility pack. Another solution did not work as well.

//meta[@name='pubdate']/data(@content)

For reasons i wanted to use just xml path (and not html agility pack functions to get the attribute value). The function i use is below:

date = TextfromOneNode(document.DocumentNode.SelectSingleNode(".//body"), "meta[@name='pubdate']/@content");
public static string TextfromOneNode(HtmlNode node, string xmlPath)
{
    string toReturn = "";
    if(node.SelectSingleNode(xmlPath) != null)
    {
        toReturn = node.SelectSingleNode(xmlPath).InnerText;
    }
    return toReturn;
}

So far it looks like there is no way to use xml path expression to get an attribute value directly. Any ideas?

Accepted Answer

There is a way using HtmlNodeNavigator :

public static string TextfromOneNode(HtmlNode node, string xmlPath)
{
    string toReturn = "";
    var navigator = (HtmlAgilityPack.HtmlNodeNavigator)node.CreateNavigator();
    var result = navigator.SelectSingleNode(xmlPath);
    if(result != null)
    {
        toReturn = result.Value;
    }
    return toReturn;
}

The following console app example demonstrates how HtmlNodeNavigator.SelectSingleNode() works with both XPath that return element and XPath that return attribute :

var raw = @"<div>
<meta name='pubdate' content='2012-08-30' />
<span>foo</span>
</div>";
var doc = new HtmlAgilityPack.HtmlDocument(); 
doc.LoadHtml(raw);

var navigator = (HtmlAgilityPack.HtmlNodeNavigator)doc.CreateNavigator();

var xpath1 = "//meta[@name='pubdate']/@content";
var xpath2 = "//span";

var result = navigator.SelectSingleNode(xpath1);
Console.WriteLine(result.Value);
result = navigator.SelectSingleNode(xpath2);
Console.WriteLine(result.Value);

dotnetfiddle demo

output :

2012-08-30
foo

Popular Answer

Using xml linq

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            string input = "<meta name=\"pubdate\" content=\"2012-08-30\" />";
            XElement meta = XElement.Parse(input);
            DateTime output = (DateTime)meta.Attribute("content");
        }
    }
}



Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Is this KB legal? Yes, learn why
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Is this KB legal? Yes, learn why