2010-03-12 58 views
0

我有一些標記包含特定類的某些HTML圖像標記。我需要的是找到所有這些圖像,在圖像周圍添加錨標籤,將錨點的href屬性設置爲圖像src值(圖像路徑),最後用新值替換圖像src值(我稱之爲一個將返回這個值的方法)。查找具有特定HTML類名稱的圖像

<p>Some text here <img src="/my/path/image.png" alt="image description" class="featured" />. Some more text and another image that should not be modified <img src="/my/path/image2.png" alt="image description" /></p> 

應該成爲。

<p>Some text here <a href="/my/path/image.png"><img src="/new/path/from/method.png" alt="image description" class="featured" /></a>. Some more text and another image that should not be modified <img src="/my/path/image2.png" alt="image description" /></p> 
+0

這可以用jQuery在客戶端完成。你只需要一次性的服務器端查找和替換樣式腳本,或者你能用jQuery在你的客戶端實現這一點嗎? – Codesleuth 2010-03-12 09:15:38

+0

需要在服務器上執行此操作,以便適用於那些沒有JavaScript或未啓用JavaScript的用戶。 – 2010-03-12 09:18:14

回答

0

不要使用RegEx來解析HTML。由於原因,請參閱this經典SO回答。使用HTML Agility Pack而不是 - 您可以使用XPath來查詢您的HTML。

+0

謝謝!將研究HTML敏捷包 – 2010-03-12 11:49:11

0

結束了這段代碼。

using System; 

using System.Reflection; 使用HtmlAgilityPack; 使用log4net;

命名空間Company.Web.Util { 公共靜態類的HTMLParser { 私人靜態只讀的ILog _log = LogManager.GetLogger(MethodBase.GetCurrentMethod()DeclaringType。); private static HtmlDocument _htmlDocument;

public static string Parse(string input) 
    { 
     _htmlDocument = new HtmlDocument(); 

     _htmlDocument.LoadHtml(input); 
     ParseNode(_htmlDocument.DocumentNode); 

     return _htmlDocument.DocumentNode.WriteTo().Trim(); 
    } 

    private static void ParseChildren(HtmlNode parentNode) 
    { 
     for (int i = parentNode.ChildNodes.Count - 1; i >= 0; i--) 
     { 
      ParseNode(parentNode.ChildNodes[i]); 
     } 
    } 

    private static void ParseNode(HtmlNode node) 
    { 
     if (node.NodeType == HtmlNodeType.Element) 
     { 
      if (node.Name == "img" && node.HasAttributes) 
      { 
       for (int i = node.Attributes.Count - 1; i >= 0; i--) 
       { 
        HtmlAttribute currentAttribute = node.Attributes[i]; 
        if ("class" == currentAttribute.Name && currentAttribute.Value.ToLower().Contains("featured")) 
        { 
         try 
         { 
          string originaleImagePath = node.Attributes["src"].Value; 

          string imageThumbnailPath = GetImageThumbnail(originaleImagePath); 

          var anchorNode = HtmlNode.CreateNode("<a>"); 
          var imageNode = HtmlNode.CreateNode("<img>"); 

          imageNode.SetAttributeValue("alt", node.Attributes["alt"].Value); 
          imageNode.SetAttributeValue("src", imageThumbnailPath); 

          anchorNode.SetAttributeValue("href", originaleImagePath); 

          anchorNode.AppendChild(imageNode); 
          node.ParentNode.InsertBefore(anchorNode, node); 

          node.ParentNode.RemoveChild(node); 
         } 
         catch (Exception exception) 
         { 
          if (_log.IsDebugEnabled) 
          { 
           _log.WarnFormat("Some message: {0}", exception); 
          } 
         } 
        } 
       } 
      } 
     } 

     if (node.HasChildNodes) 
     { 
      ParseChildren(node); 
     } 
    } 
} 

}

相關問題