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?
meta[@name='pubdate']element node and then access its attribute as["content"]in C#. - Martin Honnen