Convert uris to links in html page

S-Soft 646 Reputation points
2023-01-17T16:57:52.6766667+00:00

Hi,

I have a very simple/basic html which has some uris inside, like this:

<p>some text http://www.msn.com the rest</p>

Need to convert the uris to links like this:

http://www.msn.com/abc

to:

<a href="http://www.msn.com/abc">http://www.msn.com/abc</a>

Any embedded .NET Framework 4.0 method to do so? Thanks :)

  • Better not to be regex as might fail on rare conditions for some link formats
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,204 questions
VB
VB
An object-oriented programming language developed by Microsoft that is implemented on the .NET Framework. Previously known as Visual Basic .NET.
2,564 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Jack J Jun 24,281 Reputation points Microsoft Vendor
    2023-01-18T06:10:48.6+00:00

    @S-Soft, Welcome to Microsoft Q&A, there are two methods you could refer to to convert the url like what you wanted.

    First, you could created a winform app and drag the webbrowser control to the form.

    Then, you could try the following code to do it.

     private void Form1_Load(object sender, EventArgs e)
            {
                webBrowser1.Url = new Uri("1.html");
            }
    
            private void button1_Click(object sender, EventArgs e)
            {
                webBrowser1.Url = null;
                WebClient web = new WebClient();
                string htmlstr = web.DownloadString("1.html");
                foreach (HtmlElement item in webBrowser1.Document.All)
                {
                    if(item.TagName=="P")
                    {
                        string result = item.InnerText.Split(' ').Where(i => i.StartsWith("http") && i.Contains("www")).FirstOrDefault();
                        string str = string.Format("<a href=\"{0}\">{0}</a>", result);
                        htmlstr = htmlstr.Replace(result, str);
                    }
                }
                File.WriteAllText("1.html", htmlstr);
              
                webBrowser1.Url = new Uri("1.html");
    
    
            }
    
    

    Second, you could try to install the nuget package HtmlAgilityPack to do it.

            static void Main(string[] args)
            {
                WebClient web = new WebClient();
                string htmlstr = web.DownloadString("1.html");
                var doc = new HtmlDocument();
                doc.LoadHtml(htmlstr);
                foreach (var item in doc.DocumentNode.DescendantNodes())
                {
                    if(item.Name=="p")
                    {
                        string result = item.InnerText.Split(' ').Where(i=>i.StartsWith("http")&&i.Contains("www")).FirstOrDefault();
                        string str = string.Format("<a href=\"{0}\">{0}</a>", result);
                        htmlstr=htmlstr.Replace(result, str);
                   
                    
                    }
                 
                }
                File.WriteAllText("1.html", htmlstr);
            }
    
    

    Result:

    User's image

    Hope my solution could help you.

    Best Regards,

    Jack


    If the answer is the right solution, please click "Accept Answer" and upvote it.If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our [documentation][3] to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments