使用XML序列化序列化没有父节点的集合 [英] Use XML serialization to serialize a collection without the parent node

查看:85
本文介绍了使用XML序列化序列化没有父节点的集合的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

让我说一堂课

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

我想将此序列化为XML,这样,Passengers是Car的子节点,并且没有中间的Passengers节点.换句话说,我希望输出看起来像这样;

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>

我需要在Car.Passengers中添加一个属性,以实现此目的,但我不记得是哪个.

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

推荐答案

这就是我所追求的!

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

这篇关于使用XML序列化序列化没有父节点的集合的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆