0

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.

1 Answer 1

1

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"); } } 
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.