需要用新值替换img src属性

时间:2011-05-24 11:39:43

标签: winforms c#-4.0 html-agility-pack

我正在从SQL Server中检索许多网页(之前已保存)的HTML。我的目的是修改img的src属性。 HTML中只有一个img标记,它的来源是这样的:

... <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;
        }           
    }

我需要帮助,因为标记没有以这种方式更新,我需要将修改后的标记存储回DB。感谢。

1 个答案:

答案 0 :(得分:8)

XPATH比所有这些XLinq行话更加明智,恕我直言...... 以下是如何做到这一点:

    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));
    }

此代码搜索具有imgsrcheight属性的width代码。然后,它将替换src属性值。