views:

24

answers:

1

In my program I have a string which contains URLs separated by /n (One per line)

Let's say the string is called "links". I want to take this string and generate a HTML file that will automatically open in my default browser which will make each URL a hyperlink (one per line). How would I make such a report not using any third party components using WPF C# 4.0? I want the report to be generated by clicking a button called "Export".

+1  A: 

There are plenty of ways to do this, but here is a quick and dirty example (debugging may be necessary since I wrote this on the fly). [Edit: Now uses Uri objects to formulate the actual address.]

private void export_Click(object sender, RoutedEventArgs e)
    {
        string tempFileName = "list.html";
        string links = "http://www.google.com/#sclient=psy&hl=en&site=&source=hp&q=test+me&aq=f&aqi=&aql=&oq=&gs_rfai=&pbx=1&fp=ddfbf15c2e2f4021\nhttp://www.testme.com/Test-Prep.html?afdt=Q3RzePF0jU8KEwja-5WM7PqkAhUUiZ0KHaoG_wcYASAAMJbwoAM4MEC4w6uX7dS53gdQlvCgA1CEra8PUJzr_xNQg73wFVCKttweUJStzNoBUNv67ZsD";
        List<Uri> uriCollection = new List<Uri>();

        foreach (string url in links.Split(new char[] { '\n' }, StringSplitOptions.RemoveEmptyEntries))
        {
            uriCollection.Add(new Uri(url));
        }

        // Create temporary file.
        using (TextWriter writer = new StreamWriter(tempFileName))
        {
            try
            {
                writer.WriteLine("<html>");
                writer.WriteLine("<head><title>Links</title></head>");
                writer.WriteLine("<body>");

                writer.WriteLine("<p>");

                foreach (Uri uri in uriCollection)
                {
                    writer.WriteLine("<a href=\"{0}\">{1}</a><br />", uri.OriginalString, uri.Host);
                }

                writer.WriteLine("</p>");

                writer.WriteLine("</body>");
                writer.WriteLine("</html>");
            }
            catch (Exception ex)
            {
                System.Diagnostics.Trace.TraceError(ex.Message);
            }
            finally
            {
                writer.Close();
            }
        }

        // Open browser with temporary file.
        if (File.Exists(tempFileName))
        {
            System.Diagnostics.Process.Start(tempFileName);
        }
    }

The 'Export' button is wired to the event 'export_Click'. I hard-coded the the string with '\n''s for the example. Simply break these apart using split and write a temporary file creating the HTML you need. Then, once the file is completed, you can open it using the Process.Start() method.

Ideally this can be done using DataBinding and other elements available in WPF if the need to open a browser window was not required. This would also remove any external dependencies the program may have.

Bryan Allred
Thank you! That worked perfectly! One last question. What would be the code to add to remove some repetitive data from the urls. For example if I have '+sales+degree' as part of the end some of the URLs but I want to remove just that string?
Jason
Edited code to reflect your question. I went ahead and assumed you had the entire string like the example so creating a Uri object would be fairly safe.
Bryan Allred