3

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?


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.

4

1 回答 1

2

XmlSerializer将列表与叶节点完全分开;列表上的属性不存在- 它只是所包含数据的集合。更好的方法是:

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; }   
}

这是一个具有一组计算机并具有两个属性的对象 - 但它本身不是一个列表。使用XmlElementAttributefor 列表可以根据需要展平嵌套。请注意,为方便起见,我省略了名称空间。

从列表继承(以添加成员为目的)不会很好地工作,不仅适用于XmlSerlaizer,而且适用于广泛的序列化程序和绑定框架。

于 2013-02-11T12:35:21.443 回答