【问题标题】:Need to replace an img src attrib with new value需要用新值替换 img src 属性
【发布时间】:2011-08-31 21:00:29
【问题描述】:

我正在从 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;
        }           
    }

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

【问题讨论】:

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


    【解决方案1】:

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

    此代码搜索具有srcheightwidth 属性的img 标签。然后,它替换了src 属性值。

    【讨论】:

      猜你喜欢
      • 2012-11-16
      • 2011-11-19
      • 2012-01-12
      • 1970-01-01
      • 2012-07-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-12-06
      相关资源
      最近更新 更多