D
D
Dmitry Filandor2017-10-11 11:13:05
ASP.NET
Dmitry Filandor, 2017-10-11 11:13:05

How to replace a block of html text?

Hello! There is a site on asp mvc and a simple html editor for entering formatted text and inserting pictures. There was a need for the inserted pictures to increase (lightbox), for this you need to make a picture link with a certain attribute instead of the picture. Here's how I'm trying to change the finished html text:

HtmlAgilityPack.HtmlDocument doc = new HtmlAgilityPack.HtmlDocument();
                doc.LoadHtml(recordBL.Text); //recordBL.Text - тут хтмл текст
                 
                System.Text.StringBuilder txt = new System.Text.StringBuilder(recordBL.Text);

                var nodes = doc.DocumentNode.SelectNodes(@"//img[@src]");

                foreach (var img in nodes)
                {

                    
                    HtmlAgilityPack.HtmlAttribute att = img.Attributes["src"];
                    imgScrs.Add(att.Value);
          
          //формирую нужную ссылку/картинку
                    string readyLink = "<a href='" + att.Value + "' data-lightbox='lightbox-set'><img   src='" + att.Value + "'/></a>",
                    
          //это старая картинка
          old = img.OuterHtml;

          
          //и тут нужно заменить старую картинку, новой сформированной ссылкой-картинкой
                    recordBL.Text  = txt.Replace(old, readyLink).ToString();                    
                }

but Replace does not work, probably because in old in the debug when viewing the value (click on the magnifying glass) - old =
<img alt="" height="367" src="http://localhost:11733/Images/USERDATA/Posts/10-2017/u2t11102003.jpg" width="550">

and when viewing without clicking on the magnifying glass - joxi.ru/eAO4DQC4qbNpro
, that is, double quotes are escaped ... there are no other options why the replay does not work out
these dances with a tambourine can be done on the client side through js, but unfortunately not strong in js
-- -------UP
another option:
string readyLink = "a href=\"" + att.Value + "\" data-lightbox=\"lightbox-set\"><img   src=\"" + att.Value + "\"/></a",
                        old = img.OuterHtml.ToString();

                    HtmlAgilityPack.HtmlNode aaa = doc.CreateElement(readyLink);

                    img.ParentNode.ReplaceChild(aaa, img);                
                   
                    recordBL.Text = doc.DocumentNode.OuterHtml;

but two links are created, instead of one, there is no parent, since I only select pictures - var nodes = doc.DocumentNode.SelectNodes(@"//img[@src]");

Answer the question

In order to leave comments, you need to log in

2 answer(s)
M
mpnj, 2017-10-11
@mpnj

If you answer the question "how to bypass escaping" (well, if you have specific tastes :) ), then you can try to do not Replace, but Remove.
Those. you will find in your source text the beginning of the img tag by the substring "" (or something like that). Get from these two values ​​the length of the text that belongs to this tag.
Then do txt.Remove(startIndex, count).
Next, insert the new value with . txt.Insert(startIndex, readyLink) in place of the old tag.
Here the most "difficult" moment will be with the fact that you have several img tags, judging by the presence of foreach.
I hope I explained the idea clearly.
Well, in general, yes, the global problem itself looks strange.

F
Fat Lorrie, 2017-10-11
@Free_ze

that is, double quotes are escaped...

The debugger gives you a copy- friendly representation (if you notice, the string itself is also framed in quotes), there is actually no escaping in the variable, this feature is needed only for string literals.
Are you sure the correct usage here is StringBuilder? Now it is of no use, because at each iteration of the loop it returns and writes a string that will become unnecessary at the next iteration, i.e. memory is still clogged, allocations occur. Some premature optimization.
Make sure that the value being replaced is actually there. HtmlAgilityPack may well optimize HTML (the one from OuterHtml), say, to strip spaces and/or newlines.
If you are working with a DOM parser, then you should not reinvent the wheel, but use its API:
string htmlTag = "<foo>bar</foo>";
var newNode = HtmlNode.CreateNode( htmlTag );
img.ParentNode.ReplaceChild(newNode, img);

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question