Answer the question
In order to leave comments, you need to log in
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();
}
<img alt="" height="367" src="http://localhost:11733/Images/USERDATA/Posts/10-2017/u2t11102003.jpg" width="550">
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;
Answer the question
In order to leave comments, you need to log in
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.
that is, double quotes are escaped...
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. 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 questionAsk a Question
731 491 924 answers to any question