2011-05-24 84 views
2

我正在從SQL Server中檢索許多網頁(之前保存的)的HTML。我的目的是修改一個img的src屬性。只有一個在HTML img標籤,它的來源是像這樣:需要用新值替換img src attrib

... <td colspan="3" align="center"> <img src="/crossword/13cnum1.gif" height="360" width="360" border="1"><br></td> ...

我需要的/crossword/13cnum1.gif更改爲http://www.nostrotech.com /crossword/13cnum1.gif

代碼:

private void ReplaceTest() { 
     String currentCode = string.Empty; 

     Cursor saveCursor = Cursor.Current; 

     try { 
      Cursor.Current = Cursors.WaitCursor; 
      foreach (WebData oneWebData in DataContext.DbContext.WebDatas.OrderBy(order => order.PuzzleDate)) { 
       if (oneWebData.Status == "Done") { 

        currentCode = oneWebData.Code; 

        #region Setup Agility 
        HtmlAgilityPack.HtmlDocument AgilityHtmlDocument = new HtmlAgilityPack.HtmlDocument { 
         OptionFixNestedTags = true 
        }; 

        AgilityHtmlDocument.LoadHtml(oneWebData.PageData); 
        #endregion 

        #region Image and URL 
        var imageOnPage = from imgTags in AgilityHtmlDocument.DocumentNode.Descendants() 
                 where imgTags.Name == "img" && 
                   imgTags.Attributes["height"] != null && 
                   imgTags.Attributes["width"] != null 
                 select new { 
                  Url = imgTags.Attributes["src"].Value, 
                  tag = imgTags.Attributes["src"], 
                  Text = imgTags.InnerText 
                 }; 

        if (imageOnPage == null) { 
         continue; 
        } 

        imageOnPage.FirstOrDefault().tag.Value = "http://www.nostrotech.com" + imageOnPage.FirstOrDefault().Url;                
        #endregion     
       } 
      } 
     } 
     catch (Exception ex) { 
      XtraMessageBox.Show(String.Format("Exception: " + currentCode + "!{0}Message: {1}{0}{0}Details:{0}{2}", Environment.NewLine, ex.Message, ex.StackTrace), Text, MessageBoxButtons.OK, MessageBoxIcon.Error); 
     } 
     finally { 
      Cursor.Current = saveCursor; 
     }   
    } 

我需要幫助的標記不更新這個娃我和我需要將修改後的標記存回數據庫。謝謝。

回答

8

XPATH比這一切XLINQ行話更consise,恕我直言... 這裏是如何做到這一點:

HtmlDocument doc = new HtmlDocument(); 
    doc.Load(myHtml); 

    foreach (HtmlNode img in doc.DocumentNode.SelectNodes("//img[@src and @height and @width]")) 
    { 
     img.SetAttributeValue("src", "http://www.nostrotech.com" + img.GetAttributeValue("src", null)); 
    } 

此代碼搜索有srcheightwidth屬性img標籤。然後,它將替換src屬性值。