Clean XML serializing a hierarchical, recursive data structure

前端 未结 1 972
攒了一身酷
攒了一身酷 2021-01-01 06:19

I have this class:

[XmlRoot(\"menuItem\")]
public class MenuItem
{
    [XmlAttribute(\"text\")]
    public string Text { get; set; }

    [XmlAttribute(\"isL         


        
相关标签:
1条回答
  • 2021-01-01 06:41

    Add XmlArrayItemAttribute and take away the IsNullable:

    [XmlArray("items"), XmlArrayItem("menuItem")]
    public List<MenuItem> Items { get; set; }
    

    To get rid of the extra namespaces, you need to use XmlSerializerNamespaces:

    var ns = new XmlSerializerNamespaces();
    ns.Add("","");
    var ser = new XmlSerializer(typeof (MenuItem));
    ser.Serialize(Console.Out, obj, ns);
    
    0 讨论(0)
提交回复
热议问题