LP13
LP13

Reputation: 34109

Using Html Agility Pack how to select node with namespace

i have html

<cr:checkboxes name="name1"> 
  <cr:checkbox label="Checkbox 1" /> 
  <cr:checkbox label="Checkbox 2" /> 
  <cr:checkbox label="Checkbox 3" /> 
</cr:checkboxes>

i am using html agility to load html

 var document = new HtmlDocument();
 document.LoadHtml(htmlString);

 //select all nodes that starts with `cr:checkboxes`
document.DocumentNode.SelectNodes("//cr:checkboxes");

while selecting i am getting exception

System.Xml.XPath.XPathException: 'Namespace Manager or XsltContext needed. This query has a prefix, variable, or user-defined function.'

Typically, in any other xml document i would have solve this using XmlDocument and by adding namespace using namespace manager

How to select all nodes that has name cr:checkboxes

Upvotes: 2

Views: 816

Answers (1)

cdev
cdev

Reputation: 5371

This will solve your problem.

var nodes = doc.DocumentNode.SelectNodes("//*[name()='cr:checkboxes']");

I couldn't find another way to support XmlNameSpaceManager with HtmlAgilityPack.

Upvotes: 5

Related Questions