can i omit the intervening level when using XmlSerializer for a list?
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
精彩评论