0

I have the following class:

public class MenuItem
{
    public string Title { get; set; }

    public List<MenuItem> Items { get; set; }

    public MenuItem()
    {
        Items = new List<MenuItem>();
    }
}

How it is possible to properly serialize this?

Adrian Tarniceru

and I created a tree of MenuItem objects and wanted to serialize it with YamlDotNet but the result was unexpected.

        MenuItem _root = new MenuItem() { Title = "Menu" };
        MenuItem childItem1 = new MenuItem() { Title = "Child item #1" };
        childItem1.Items.Add(new MenuItem() { Title = "Child item #1.1" });
        childItem1.Items.Add(new MenuItem() { Title = "Child item #1.2" });
        _root.Items.Add(childItem1);
        _root.Items.Add(new MenuItem() { Title = "Child item #2" });

       var serializer = new Serializer();

        string fileContent = serializer.Serialize(_root);

        using (StreamWriter writer = new StreamWriter("Menu.yaml"))
        {
            writer.Write(fileContent);
        }

result was:


...

bu I expected a tree of MenuItems in Yaml.

Tarniadi
  • 79
  • 1
  • 1
  • 4
  • 1
    Cannot reproduce. Your code generates the expected YAML. Maybe you're checking the wrong file? – flyx Aug 16 '19 at 09:59
  • I read the generate file as it is only one. Can you give me your solution code to check? – Tarniadi Aug 21 '19 at 13:39
  • I really just pasted your code to dotnetfiddle, [see here](https://dotnetfiddle.net/D0BggW). – flyx Aug 21 '19 at 14:41
  • Thanks a lot. My example is included in an application. I will check the impact. With your simple code it worked. – Tarniadi Aug 22 '19 at 17:44

0 Answers0