简体   繁体   中英

Use XML serialization to serialize a collection without the parent node

Let's say I have a class;

public class Car 
{ 
  public List<Passenger> Passengers {get; set;} 
}

I want to serialize this to XML such that Passengers are child nodes of Car and there is no intervening Passengers node. In other words I want the output to look like this;

<Car>
  <Passenger>...</Passenger>
  <Passenger>...</Passenger>
</Car>

and not like this, which is the default layout;

<Car>   
  <Passengers>
    <Passenger>...</Passenger>
    <Passenger>...</Passenger>
  </Passengers> 
</Car>

There's an attribute I need to add to Car.Passengers to achieve this, I don't recall which though.

This is what I was after!

 [XmlElement("Passenger")] public List<Passenger> Passengers {get; set;} 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM