MentalBrake
MentalBrake

Reputation: 177

C# XML Serialization - How to Serialize attribute in Class that inherits List<Object>?

I need to create an XML file using C#. I am using a class that inherits List that represents a list of computers and later initialize it with values but the serializer doesn't get the attributes for this class, only for its descendants. this is the class:

public class Computers : List<Computer>
    {
        [XmlAttribute("StorageType")]
        public int StorageType { get; set; }


        [XmlAttribute("StorageName")]
        public string StorageName { get; set; }            

    }

    public class Computer 
    {
        [XmlAttribute("StorageType")]
        public int StorageType { get; set; }

        [XmlAttribute("StorageName")]
        public string StorageName { get; set; }

        public string IPAddress { get; set; }
        public string Name { get; set; }
    }

The result should look something like this:

<fpc4:Computers StorageName="Computers" StorageType="1">
    <fpc4:Computer StorageName="{D37291CA-D1A7-4F34-87E4-8D84F1397BEA}" StorageType="1">
        <fpc4:IPAddress dt:dt="string">127.0.0.1</fpc4:IPAddress>
        <fpc4:Name dt:dt="string">Computer1</fpc4:Name>
    </fpc4:Computer>
    <fpc4:Computer StorageName="{AFE5707C-EA71-4442-9CA8-2A6264EAA814}" StorageType="1">
        <fpc4:IPAddress dt:dt="string">127.0.0.1</fpc4:IPAddress>
        <fpc4:Name dt:dt="string">Computer2</fpc4:Name>
    </fpc4:Computer>

But what I get so far is this:

<fpc4:Computers>
    <fpc4:Computer StorageType="1" StorageName="{7297fc09-3142-4284-b2e9-d6ea2fb1be78}">
      <fpc4:IPAddress>127.0.0.1</fpc4:IPAddress>
      <fpc4:Name>Computer1</fpc4:Name>
    </fpc4:Computer>
    <fpc4:Computer StorageType="1" StorageName="{eab517f6-aca9-4d01-a58b-143f2e3211e7}">
      <fpc4:IPAddress>127.0.0.1</fpc4:IPAddress>
      <fpc4:Name>Computer2</fpc4:Name>
    </fpc4:Computer>
  </fpc4:Computers>

As you can see the Computers node which is the parent node doesn't get the attributes.

Do you guys have a solution?

Upvotes: 3

Views: 8010

Answers (1)

Marc Gravell
Marc Gravell

Reputation: 1062650

XmlSerializer treats lists completely separate to leaf nodes; properties on lists do not exist - it is just a collection of the contained data. A better approach would be:

public class Computers {
    private readonly List<Computer> items = new List<Computer>();
    [XmlElement("Computer")]
    public List<Computer> Items { get { return items; } }

    [XmlAttribute("StorageType")]
    public int StorageType { get; set; }

    [XmlAttribute("StorageName")]
    public string StorageName { get; set; }   
}

This is an object that has a set of computers and has two attributes - but is not a list itself. The use of XmlElementAttribute for the list flattens the nesting as desired. Note that I have omitted namespaces for convenience.

Inheriting from a list (with an aim of adding members) will not work well, not just for XmlSerlaizer, but for a wide range of serializers and binding frameworks.

Upvotes: 2

Related Questions