HTML列表的字符串操作

本文关键字:操作 字符串 列表 HTML | 更新日期: 2023-09-27 17:51:18

我有这个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";

如何把它变成:

<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>

现在我使用的是ViewHelper,但它完全无法处理|||的东西:

@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>
    }
}

谢谢你的帮助。

HTML列表的字符串操作

像这样:

        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>"
        ;