I need to convert all src attributes to point to the absolute path instead of relative path. I tried this using HTML Agility:
string html = "<body><div><img src=\"/folder/a.png\"/></div><div> <img src=\"/folder/b.png\"/></div></body>";
var htmlDoc = new HtmlDocument();
htmlDoc.LoadHtml(html);
foreach (var node in htmlDoc.DocumentNode.SelectNodes("//img"))
{
var src = node.Attributes[@"src"].Value;
if (src.StartsWith("/"))
node.SetAttributeValue("//src", "www.abc.xyz" + src);
}
var newHtml = htmlDoc.Text;
But src attributes still points to the relative path!
You have to use htmlDoc.DocumentNode.WriteTo()
method to reflect your changes in your original html
string html = "<body><div><img src=\"/folder/a.png\"/></div><div> <img src=\"/folder/b.png\"/></div></body>";
var htmlDoc = new HtmlDocument();
htmlDoc.LoadHtml(html);
foreach (var node in htmlDoc.DocumentNode.SelectNodes("//img"))
{
var src = node.Attributes[@"src"].Value;
if (src.StartsWith("/"))
node.SetAttributeValue("src", "www.abc.xyz" + src);
}
var newHtml = htmlDoc.DocumentNode.WriteTo();
Output:
Try once may it help you