Anonymous user
CSV to HTML translation: Difference between revisions
added C#
m (→Simple solution: typo) |
(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><angry>Now you listen here! He's not the messiah; he's a very naughty boy! Now go away!</angry></td></tr><tr><td>The multitude</td><td>Who are you?</td></tr><tr><td>Brians mother</td><td>I'm his mother; that's who!</td></tr><tr><td>The multitude</td><td>Behold his mother! Behold his mother!</td></tr></table>
</lang>
|