开发者

can i omit the intervening level when using XmlSerializer for a list?

开发者 https://www.devze.com 2023-02-10 15:53 出处:网络
my question is best described by a simple example. consider 2 classes like this: class Order { [XmlAttribute] int orderId;

my question is best described by a simple example. consider 2 classes like this:

class Order {
  [XmlAttribute] int orderId;
  [XmlAttribute] int customerId;
  List<OrderItem> 开发者_如何学Citems;
}

class OrderItem {
  [XmlAttribute] int partCode;
  [XmlAttribute] int quantity;
}

using XmlSerializer, this will serialize to something like this:

<order orderId="...", customerId="..." >
  <Items>
    <orderItem partCode="..." quantity="..." />
  </Items>
</order>

what I want to do is remove the <Items> level so that the <orderItem> elements go straight underneath the corresponding <order>

is there any way to do this?


Use the XmlElement attribute:

class Order {
  [XmlAttribute] int orderId;
  [XmlAttribute] int customerId;
  [XmlElement]
  List<OrderItem> items;
}

With this attribute you can also specify a custom element name for the OrderItem objects, or even a different element name for each sub-type of OrderItem

0

精彩评论

暂无评论...
验证码 换一张
取 消