A new value for an image src attrib is required.

c#-4.0 html-agility-pack winforms


I'm getting HTML from several previously stored sites from SQL Server. The src property of an image is what I want to change. In the HTML, there is just one image element, and its source is as follows:

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

The /crossword/13cnum1.gif has to be changed to the 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


                    #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) {

                    imageOnPage.FirstOrDefault().tag.Value = "http://www.nostrotech.com" + imageOnPage.FirstOrDefault().Url;                                                            
        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 assistance since the markup is NOT changed in this manner, and I must return the modified markup to the database. Thanks.

5/24/2011 11:39:42 AM

Accepted Answer

In my opinion, XPATH is far more concise than this XLinq lingo. Here's how to go about it:

    HtmlDocument doc = new HtmlDocument();

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

This program looks forimg tags that includesrc , height and width characteristics. Then, it swaps out thesrc attribute worth

5/24/2011 2:46:42 PM

Related Questions


Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow