2016-03-05 172 views
0

我有一個使用過時的texteditor的web表單。有了這個,每當用戶在設計視圖中插入圖像時,html視圖將會生成2個寬度/高度屬性。將其發佈到服務器時,會導致使用System.Xml.XPath.XPathDocument的其他頁面出現問題。如何使用正則表達式去除img標籤的額外屬性?

由於某些原因,我不能用現代的替換texteditor,因此希望從服務器端的張貼的img標籤中刪除額外的屬性(寬度和高度)。

以下是發佈HTML文本到服務器的例子:

<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> 

有沒有用vb.net正則表達式來刪除它們反正有效的方法?或者,你有更好的想法來處理這個問題?

+2

是啊,有一個更好的辦法 - 使用HTML解析器像HTMLAgilityPack。 –

+0

謝謝FᴀʀʜᴀɴAɴᴀᴍ。 HTMLAgilityPack完成了這項工作! – rorfun

回答

1

使用HTMLAgilityPack刪除重複的屬性(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() 
相關問題