I need to create a newsletters by URL. To do that, I:
WebClient
.DownloadData
to get a source of page in byte array;However, I have some troubles with paths. All elements' sources were relative (/img/welcome.png
) but I need an absolute one, like http://www.example.com/img/welcome.png.
How can I do this?
One of the possible ways to resolve this task is the use the HtmlAgilityPack library.
Some example (fix links):
WebClient client = new WebClient();
byte[] requestHTML = client.DownloadData(sourceUrl);
string sourceHTML = new UTF8Encoding().GetString(requestHTML);
HtmlDocument htmlDoc = new HtmlDocument();
htmlDoc.LoadHtml(sourceHTML);
foreach (HtmlNode link in htmlDoc.DocumentNode.SelectNodes("//a[@href]"))
{
if (!string.IsNullOrEmpty(link.Attributes["href"].Value))
{
HtmlAttribute att = link.Attributes["href"];
att.Value = this.AbsoluteUrlByRelative(att.Value);
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With