需要用新值替换img src属性 [英] Need to replace an img src attrib with new value

查看:110
本文介绍了需要用新值替换img src属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在从SQL Server检索许多网页的HTML(之前保存过)。我的目的是修改img的src属性。 HTML中只有一个img标签,其来源如下:

I'm retrieving HTML of many webpages (saved earlier) from SQL Server. My purpose is to modify an img's src attribute. There is only one img tag in the HTML and it's source is like so:

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

I need to change the /crossword/13cnum1.gif to 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;
        }           
    }

我需要帮助,因为标记不是通过这种方式更新的并且我需要将修改后的标记存储回数据库。谢谢。

I need help as the markup is NOT updated this way and I need to store the modified markup back to the DB. Thanks.

推荐答案

XPATH比所有这些XLinq术语更为简洁,恕我直言...
这是如何做到这一点:

XPATH is much more consise than all this XLinq jargon, IMHO... Here is how to do it:

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

此代码搜索 img src height width 属性的c>标签。然后,它替换 src 属性值。

This code searches for img tags that have src, height and width attributes. Then, it replaces the src attribute value.

这篇关于需要用新值替换img src属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆