1

How to write a C# class that has repeated property? For example here is how the serialized object of this class should look like.

<Vehicle>
  <Name>Forcus</Name>
  <Name>Tomry</Name>
  <Name>Hovic</Name>
  <Name>Nima</Name>
</Vehicle>
4

2 回答 2

5

You cannot have multiple properties with the same name. But you can have a collection property, of type string[] or List<string> to store multiple values.

As a concrete example:

public class Vehicle
{
    [XmlElement("Name")]
    public List<Vehicle> Names {get { return names; }}

    private readonly List<Vehicle> names = new List<Vehicle>();
}
于 2013-11-11T09:37:35.390 回答
3

just add Name(s) to collection

public class Vehicle 
{
   public List<string> Name {get;set;}
}
于 2013-11-11T09:37:26.043 回答