Looking at the Microsoft article on XML Serialization:
https://msdn.microsoft.com/en-us/library/58a18dwa.aspx
They give an example under "Serializing an Array of Objects" as below:
public class PurchaseOrder
{
public Item [] ItemsOrders
}
public class Item
{
public string ItemID
public decimal ItemPrice
}
With output:
<PurchaseOrder>
<Items>
<Item>
<ItemID>aaa111</ItemID>
<ItemPrice>34.22</ItemPrice>
</Item>
<Item>
<ItemID>bbb222</ItemID>
<ItemPrice>2.89</ItemPrice>
</Item>
</Items>
</PurchaseOrder>
What bothers me is the "Items" tag. Seems to me like only the "Item" tag should be a child of "PurchaseOrder". The "Items" tag seems unnecessary and confusing. I could be wrong.
Is there a way to get this example to serialize like this:
<PurchaseOrder>
<Item>
<ItemID>aaa111</ItemID>
<ItemPrice>34.22</ItemPrice>
</Item>
<Item>
<ItemID>bbb222</ItemID>
<ItemPrice>2.89</ItemPrice>
</Item>
</PurchaseOrder>
You can control the serialization using attributes.From "Controlling XML Serialization using Attributes": To remove the element which stands for the entire array, use the
[XmlElement]
attribute:this produces