2016-03-05 7 views
0

J'ai un formulaire web en utilisant un textteditor obsolète. Avec cela, chaque fois que l'utilisateur insère une image en mode Création, la vue html aura 2 attributs width/height générés. Lorsqu'il est publié sur le serveur, il provoque des problèmes dans d'autres pages où System.Xml.XPath.XPathDocument est utilisé.Comment supprimer les attributs supplémentaires de la balise img en utilisant une expression régulière?

Pour une raison certaine, je ne peux pas remplacer le texteditor par un moderne et je souhaite donc supprimer l'attribut supplémentaire (width et height) de la balise img publiée côté serveur.

Voici un exemple du texte HTML affiché au serveur:

<table align="left"> 
    <thead> 
    </thead> 
    <tbody> 
     <tr> 
      <td style="width: 250px; vertical-align: top; text-align: left;"> 
      <p>&nbsp;<img width="263" height="175" alt="" style="height: 122px; width: 184px;" src="/Portals/0/Resources/Site/Signature/1.PNG" width="263" height="175" /></p> 
      </td> 
      <td style="width: 50px;">&nbsp;</td> 
      <td style="width: 250px; vertical-align: top; text-align: left;"> 
      <p>&nbsp;</p> 
      <p><img width="168" height="66" alt="" style="height: 79px; width: 170px;" src="/Portals/0/Resources/Site/Signature/2.jpg" width="168" height="66" /></p> 
      </td> 
      <td style="width: 50px;">&nbsp;</td> 
      <td style="width: 250px; vertical-align: top; text-align: left;"> 
      <p>&nbsp;</p> 
      <p><img width="217" height="93" alt="" src="/Portals/0/Resources/Site/Signature/3.png" width="217" height="93" /></p> 
      </td> 
     </tr> 
    </tbody> 
</table> 

Y at-il moyen de toute façon efficace de les supprimer en utilisant l'expression régulière de vb.net? Ou, vous avez une meilleure idée de gérer cela?

+2

Oui, il y a une meilleure façon - utiliser un HTML Parser comme HTMLAgilityPack. –

+0

Merci Fᴀʀʜᴀɴ Aɴᴀᴍ. HTMLAgilityPack a fait le travail! – rorfun

Répondre

1

Utilisez HTMLAgilityPack pour supprimer les attributs dupliqués (VB.NET):

 Dim txtTemp As string = "" 
    Try 
     Dim htmlDocument as HtmlDocument = New HtmlDocument() 
     htmlDocument.LoadHtml(txtPosted)    
     for each imgNode as HtmlNode In htmlDocument.DocumentNode.Descendants("img") 
    'Get value of width/height attribute 
      Dim txtTempWidth as string = IIf(imgNode.Attributes.Contains("width"), imgNode.Attributes("width").Value, "").ToString() 
      Dim txtTempHeight as string = IIf(imgNode.Attributes.Contains("height"), imgNode.Attributes("height").Value, "").ToString()     
      if not string.IsNullOrEmpty(txtTempWidth) then 
     'remove all "width" attributes 
       While imgNode.Attributes.Contains("width") 
        imgNode.Attributes.Remove("width") 
       End While 
     'add one "width" attribute 
       imgNode.Attributes.Add("width", txtTempWidth) 
      End If 
      if not string.IsNullOrEmpty(txtTempHeight) then 
     'remove all "height" attributes 
       While imgNode.Attributes.Contains("height") 
        imgNode.Attributes.Remove("height") 
       End While 
     'add one "height" attribute 
       imgNode.Attributes.Add("height", txtTempHeight) 
      End If  
     Next 

    'close img tag 
     if (HtmlNode.ElementsFlags.ContainsKey("img")) then 
      HtmlNode.ElementsFlags("img") = HtmlElementFlag.Closed 
     else 
      HtmlNode.ElementsFlags.Add("img", HtmlElementFlag.Closed) 
     end if 

     using writer as StringWriter = new StringWriter() 
      htmlDocument.Save(writer) 
      txtTemp = writer.ToString() 
     End Using 
    Catch ex As Exception 
     Exceptions.LogException(ex) 
     txtTemp = "" 
    End Try 


    'final result 
    Dim txtFinal as string = IIf(string.IsNullOrEmpty(txtTemp), txtPosted, txtTemp) .ToString()