XML serialization of a list with attributes XML serialization of a list with attributes asp.net asp.net

XML serialization of a list with attributes


You need to make another class which represents the type element. Then you can add properties to it for the attributes, like this:

[XmlRoot(ElementName = "embellishments", IsNullable = false)]public class EmbellishmentGroup{    [XmlElement("type")]    public MyType Type { get; set; }    public EmbellishmentGroup()     {        Type = new MyType();    }}public class MyType{    [XmlAttribute("id")]    public int Id { get; set; }    [XmlAttribute("name")]    public string Name { get; set; }    [XmlElement("row")]    public List<Product> List { get; set; }    public MyType()    {        Id = 1;        Name = "bar bar foo";        List = new List<Product>();        Product p = new Product();        p.Id = 1;        p.Name = "foo bar";        p.Cost = 10m;        List.Add(p);    }}public class Product{    [XmlElement( "id" )]    public int Id { get; set; }    [XmlElement( "name" )]    public string Name { get; set; }    [XmlElement( "cost" )]    public decimal Cost { get; set; }}