Question

Given the following...

HtmlNode myDiv = doc.DocumentNode.SelectSingleNode("//div[@id='someid']");

...where the resulting myDiv.InnerHtml contains:

<span>...other content I want to consume...</span>
<a href="http://www.somewhere.com" onmousedown="return somefunc('random','parm','values','SHXA213')">Click Me</a>
<span>...and more content I want to consume...</span>

Is there a way to not select the onmousedown portion of the anchor tag?

Solution
What I needed to do was the following:

HtmlNodeCollection anchors = myDiv.SelectNodes(@"//a[@class='someclass']");
anchors[0].SetAttributeValue("onmousedown", "");

// could have also used anchors[0].Attributes.Remove() or .RemoveAt()
Was it helpful?

Solution

Is there a way to not select the onmousedown portion of the anchor tag?

No. Not with XPath (SelectSingleNode).

XPath is a query language and it cannot modify the nodes selected by an XPath expression. You need an additional language (DOM or XSLT) to change nodes (eg. strip off attributes).

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top