I am relitively new to coding c# for web applications, but have some knowledge in c#.
I am trying to read a text file (example below) and produce a webpage with format based on the characters used to seperate each line.
I’m sure there is a very simple way to do this, but this is what I have come up with. It works for my current intended purpose, but looks horrible and complicated.
The file I’m reading looks as follows:
^This ^is ^the ^home ^file.|*This is a ^bullet point.|*^This is another one.|This isn't a bullet point.
The code I have to read this and display it looks like this:
@{int bulletNum = 0;}
@foreach (string homeLine in homeData)
{
foreach (string homeItem in homeLine.Split('|'))
{
if (homeItem.First()=='*')
{
bulletNum++;
<ol class="round">
<li class="num@(bulletNum)">
@foreach (string homeElementFirst in homeItem.Split(' '))
{
string homeElementSecond = homeElementFirst.TrimStart('*');
if (homeElementSecond.First() == '^')
{
<b>@homeElementSecond.TrimStart('^')</b>
}
else
{
@homeElementSecond <text> </text>
}
}
</li>
</ol>
}
else
{
foreach (string homeElement in homeItem.Split(' '))
{
if (homeElement.First() == '^')
{
<b>@homeElement.TrimStart('^')</b>
}
else
{
@homeElement <text> </text>
}
}
}
<br />
}
}
Please excuse my terrible variable names.
Can anyone help tidy this up for me? Thanks.
It looks like you’re trying to implement a custom markup language, albeit a simple one. I would recommend that you use an existing library for this sort of thing. Check out Textile, which does something similar. There’s a C# port of Textile available.