Reputation: 8349
I am trying to figure out how to remove a specific style value from nodes.
I am using element.Attributes.Remove(element.Attributes["style"]);
to remove all styles but I only want to remove styles that have a certain value.
i.e.
Remove style from
<tr style='background-color:rgb(255, 255, 153);'>
But not from
<tr style='background-color:rgb(0, 0, 255);'>
I would also then like to add a class to the same node.
Upvotes: 0
Views: 1067
Reputation: 2372
You can't select Attributes in HAP using xpath, you can only select their elements, so the best way to do it is to actually select the elements that has the attribute with the value that you want, for example the following xpath will select all elements that has the style attribute with the given value.
//*[@style='background-color:rgb(255, 255, 153);']
so the way to go would be:
var allElementsWithStyleAttributeValue = html.DocumentNode.SelectNodes("//*[@style='background-color:rgb(255, 255, 153);']");
if(allElementsWithStyleAttributeValue!=null)
{
foreach(var el in allElementsWithStyleAttributeValue)
{
el.Attributes.Remove("style");
el.Attributes.Add("class", "youclassvalue");
}
}
Upvotes: 1