Reputation: 30208
I have this string
:
var audit = "Id:1;Name:John;Address:|||Id:123;Street:123 Main St.;City:LA;State:|||Id:234;Name:California;Code:CA|||;ZipCode:12345|||;IsActive:true";
How do I turn it into:
<ul>
<li>Id: 1<li>
<li>Name: John<li>
<li>
Address:
<ul>
<li>Id: 123<li>
<li>Street: 123 Main St.<li>
<li>City: LA<li>
<li>
State:
<ul>
<li>Id: 234<li>
<li>Name: California<li>
<li>Code: CA<li>
</ul>
<li>
<li>ZipCode: 12345</li>
</ul>
<li>
<li>IsActive: true<li>
</ul>
Right now I am using a ViewHelper
but it fails completely on dealing with the |||
stuff:
@helper ChangedDisplay(string changed)
{
if (string.IsNullOrEmpty(changed) || changed.Trim().ToLower() == "n/a")
{
@:n/a
}
else
{
var rows = changed.Split(';');
<ul>
@foreach (var row in rows)
{
var columns = row.Split(':');
<li>
@(columns.First()):
@if (columns.Last().Contains("|||"))
{
@ChangedDisplay(columns.Last().Trim('|'))
}
else
{
@columns.Last()
}
</li>
}
</ul>
}
}
thanks for your help.
Upvotes: 1
Views: 359
Reputation: 3634
Like this:
audit = "Id:1;Name:John;Address:|||Id:123;Street:123 Main St.;City:LA;State:|||Id:234;Name:California;Code:CA|||;ZipCode:12345|||;IsActive:true";
var auditHtml = "<ul><li>" + audit
.Replace("|||;", "</li></ul><li>")
.Replace(":|||", ":<ul><li>")
.Replace(";", "</li><li>")
+ "</li></ul>"
;
Upvotes: 2