C# 使用递归从 List 创建 HTML 无序列表

发布于 2024-09-12 21:03:05 字数 2039 浏览 2 评论 0原文

是否可以使用递归输出以下 HTML 无序列表。

<ul>
    <li>1
        <ul>
            <li>5
                <ul>
                    <li>8</li>
                    <li>9</li>
                </ul>
            </li>
            <li>6</li>
        </ul>
    </li>
    <li>2</li>
    <li>3</li>
    <li>4</li>
    <li>7</li>
</ul>

原始数据保存在以下列表中,

        List<Page> pages = new List<Page>();

        pages.Add(new Page { Id = 1, pageId = 1, parentPageId = 0 });
        pages.Add(new Page { Id = 2, pageId = 2, parentPageId = 0 });
        pages.Add(new Page { Id = 3, pageId = 3, parentPageId = 0 });
        pages.Add(new Page { Id = 4, pageId = 4, parentPageId = 0 });
        pages.Add(new Page { Id = 5, pageId = 5, parentPageId = 1 });
        pages.Add(new Page { Id = 6, pageId = 6, parentPageId = 1 });
        pages.Add(new Page { Id = 7, pageId = 7, parentPageId = 0 });
        pages.Add(new Page { Id = 8, pageId = 8, parentPageId = 5 });
        pages.Add(new Page { Id = 9, pageId = 9, parentPageId = 5 });

其中页面是

    public class Page
    {
        public int Id { get; set; }
        public int pageId { get; set; }
        public int parentPageId { get; set; }
    }

这是我到目前为止所拥有的,当然这并不完全有效......

        foreach (var page in pages)
        {
            Console.WriteLine("<ul><li>{0}", page.pageId);
            WriteMenu(pages, page.pageId, page.parentPageId);  
        }


    public static void WriteMenu(List<Page> pages, int pageId, int parentPageId)
    {
        foreach (var page in pages)
        {
            if (page.parentPageId == pageId)
            {
                Console.WriteLine("<ul><li>{0}", page.pageId);
                WriteMenu(pages, page.pageId, page.parentPageId);
            }
        }
    }

Is it possible to output the following HTML unordered list using recursion.

<ul>
    <li>1
        <ul>
            <li>5
                <ul>
                    <li>8</li>
                    <li>9</li>
                </ul>
            </li>
            <li>6</li>
        </ul>
    </li>
    <li>2</li>
    <li>3</li>
    <li>4</li>
    <li>7</li>
</ul>

Where the original data is is held in the following list

        List<Page> pages = new List<Page>();

        pages.Add(new Page { Id = 1, pageId = 1, parentPageId = 0 });
        pages.Add(new Page { Id = 2, pageId = 2, parentPageId = 0 });
        pages.Add(new Page { Id = 3, pageId = 3, parentPageId = 0 });
        pages.Add(new Page { Id = 4, pageId = 4, parentPageId = 0 });
        pages.Add(new Page { Id = 5, pageId = 5, parentPageId = 1 });
        pages.Add(new Page { Id = 6, pageId = 6, parentPageId = 1 });
        pages.Add(new Page { Id = 7, pageId = 7, parentPageId = 0 });
        pages.Add(new Page { Id = 8, pageId = 8, parentPageId = 5 });
        pages.Add(new Page { Id = 9, pageId = 9, parentPageId = 5 });

where Page is

    public class Page
    {
        public int Id { get; set; }
        public int pageId { get; set; }
        public int parentPageId { get; set; }
    }

Here's what I have so far which of course doesn't completely work...

        foreach (var page in pages)
        {
            Console.WriteLine("<ul><li>{0}", page.pageId);
            WriteMenu(pages, page.pageId, page.parentPageId);  
        }


    public static void WriteMenu(List<Page> pages, int pageId, int parentPageId)
    {
        foreach (var page in pages)
        {
            if (page.parentPageId == pageId)
            {
                Console.WriteLine("<ul><li>{0}", page.pageId);
                WriteMenu(pages, page.pageId, page.parentPageId);
            }
        }
    }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

审判长 2024-09-19 21:03:05

怎么样:

public static bool WriteMenu(List<Page> pages, int parentId, int indent)
{
    string padding = new string(' ', indent * 8);
    bool writtenAny = false;
    foreach (var page in pages.Where(p => p.ParentPageId == parentId))
    {
        if (!writtenAny)
        {                
            Console.WriteLine();
            Console.WriteLine(padding + "<ul>");
            writtenAny = true;
        }
        Console.Write(padding + "    <li>{0}", page.PageId);
        if (WriteMenu(pages, page.PageId, indent + 1))
        {
            Console.WriteLine(padding + "    </li>");                
        }
        else
        {
            Console.WriteLine("</li>");
        }
    }        
    if (writtenAny)
    {
        Console.WriteLine(padding + "</ul>");
    }
    return writtenAny;
}    
...
WriteMenu(pages, 0, 0);

How about:

public static bool WriteMenu(List<Page> pages, int parentId, int indent)
{
    string padding = new string(' ', indent * 8);
    bool writtenAny = false;
    foreach (var page in pages.Where(p => p.ParentPageId == parentId))
    {
        if (!writtenAny)
        {                
            Console.WriteLine();
            Console.WriteLine(padding + "<ul>");
            writtenAny = true;
        }
        Console.Write(padding + "    <li>{0}", page.PageId);
        if (WriteMenu(pages, page.PageId, indent + 1))
        {
            Console.WriteLine(padding + "    </li>");                
        }
        else
        {
            Console.WriteLine("</li>");
        }
    }        
    if (writtenAny)
    {
        Console.WriteLine(padding + "</ul>");
    }
    return writtenAny;
}    
...
WriteMenu(pages, 0, 0);
疯到世界奔溃 2024-09-19 21:03:05

抱歉,我第一次尝试时错过了递归位,这应该会得到您想要的结果:

        Console.WriteLine("<ul>");
        foreach (var page in pages)
        {
            if (page.parentPageId == 0)
            {
                WriteMenu(pages, page);
            }
        }
        Console.WriteLine("</ul>");



    private static void WriteMenu(List<Page> pages, Page page)
    {
        Console.WriteLine("<li>" + page.pageId);

        var subpages = pages.Where(p => p.parentPageId == page.pageId);

        if (subpages.Count() > 0)
        {
            Console.WriteLine("<ul>");
            foreach (Page p in subpages)
            {
                if (pages.Count(x => x.parentPageId == p.pageId) > 0)
                    WriteMenu(pages, p);
                else
                    Console.WriteLine(string.Format("<li>{0}</li>", p.pageId));
            }
            Console.WriteLine("</ul>");
        }

        Console.WriteLine("</li>");
    }

Sorry, missed the recursive bit on my first stab, this should get the results you want:

        Console.WriteLine("<ul>");
        foreach (var page in pages)
        {
            if (page.parentPageId == 0)
            {
                WriteMenu(pages, page);
            }
        }
        Console.WriteLine("</ul>");



    private static void WriteMenu(List<Page> pages, Page page)
    {
        Console.WriteLine("<li>" + page.pageId);

        var subpages = pages.Where(p => p.parentPageId == page.pageId);

        if (subpages.Count() > 0)
        {
            Console.WriteLine("<ul>");
            foreach (Page p in subpages)
            {
                if (pages.Count(x => x.parentPageId == p.pageId) > 0)
                    WriteMenu(pages, p);
                else
                    Console.WriteLine(string.Format("<li>{0}</li>", p.pageId));
            }
            Console.WriteLine("</ul>");
        }

        Console.WriteLine("</li>");
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文