css
  • html-agility-pack
  • 2016-02-29 30 views 0 likes 
    0

    我想弄清楚如何從節點中刪除特定的樣式值。 我正在使用element.Attributes.Remove(element.Attributes["style"]);刪除所有樣式,但我只想刪除具有特定值的樣式。從使用HtmlAgilityPack移除具有特定值的屬性?

    <tr style='background-color:rgb(255, 255, 153);'> 
    

    刪除的風格,但不是從

    <tr style='background-color:rgb(0, 0, 255);'> 
    

    我還要再像一類添加到同一節點。

    回答

    1

    您無法使用xpath選擇HAP中的屬性,只能選擇其元素,因此最好的方法是實際選擇具有您想要的值的屬性的元素,例如以下內容xpath將選擇具有給定值的style屬性的所有元素。

    //*[@style='background-color:rgb(255, 255, 153);'] 
    

    所以要走的路將是:

    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"); 
        } 
        } 
    
    相關問題