CSV to HTML translation: Difference between revisions

added C#
(added C#)
Line 334:
<TR><TD>The multitude</TD><TD>Behold his mother! Behold his mother!</TD></TR>
</TABLE>
</lang>
 
=={{header|C sharp}}==
<lang C sharp>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
 
class Program
{
private static string ConvertCsvToHtmlTable(string csvText)
{
//split the CSV, assume no commas or line breaks in text
List<List<string>> splitString = new List<List<string>>();
List<string> lineSplit = csvText.Split('\n').ToList();
foreach (string line in lineSplit)
{
splitString.Add(line.Split(',').ToList());
}
 
//encode text safely, and create table
string tableResult = "<table>";
foreach(List<string> splitLine in splitString)
{
tableResult += "<tr>";
foreach(string splitText in splitLine)
{
tableResult += "<td>" + WebUtility.HtmlEncode(splitText) + "</td>";
}
tableResult += "</tr>";
}
tableResult += "</table>";
return tableResult;
}
}
</lang>
 
Output when using the text suggested:
<lang html5>
<table><tr><td>Character</td><td>Speech</td></tr><tr><td>The multitude</td><td>The messiah! Show us the messiah!</td></tr><tr><td>Brians mother</td><td>&lt;angry&gt;Now you listen here! He&#39;s not the messiah; he&#39;s a very naughty boy! Now go away!&lt;/angry&gt;</td></tr><tr><td>The multitude</td><td>Who are you?</td></tr><tr><td>Brians mother</td><td>I&#39;m his mother; that&#39;s who!</td></tr><tr><td>The multitude</td><td>Behold his mother! Behold his mother!</td></tr></table>
</lang>
 
Anonymous user