Selecting attribute value using XPath and HtmlAgilityPack

c# html html-agility-pack xml xpath

Question

Using an xpath expression in the HTML Agility Pack, I'm attempting to get the value of the second property of a meta tag: Using a meta tag,

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

I'm using this xml path expression:

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

But it gives back nothing. I looked about and attempted to put this remedy into practice:

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

Another approach is:

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

But the HTML Agility Pack throws an XML error. A other approach did not perform as well.

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

For various reasons, I only wanted to utilize xml paths (and not html agility pack functions to get the attribute value). I utilize the function listed 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 seems that there is no straightforward method to get an attribute value using an xml path expression. Any thoughts?

1
1
8/22/2016 12:11:48 AM

Accepted Answer

A technique usingHtmlNodeNavigator :

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 sample console program shown below showsHtmlNodeNavigator.SelectSingleNode() Uses XPath that return attribute as well as XPath that return element:

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
0
8/22/2016 12:09:05 AM

Popular Answer

XML Linq use

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


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow