简体   繁体   中英

XML Serialization List of Objects

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:

 public class Group{ [XmlElement] public Employee[] Employees; } 

this produces

 <Group> <Employees> <Name>Haley</Name> </Employees> <Employees> <Name>Noriko</Name> </Employees> <Employees> <Name>Marco</Name> </Employees> </Group> 

You can use the XmlElement attribute to specify the name you want to use for each item:

namespace ConsoleApplication1
{
    using System;
    using System.IO;
    using System.Xml.Serialization;

    internal class Program
    {
        private static void Main(string[] args)
        {
            var order = new PurchaseOrder { ItemsOrders = new Item[2] };

            order.ItemsOrders[0] = new Item { ItemID = "1", ItemPrice = 1723 };
            order.ItemsOrders[1] = new Item { ItemID = "2", ItemPrice = 4711 };

            var serializer = new XmlSerializer(typeof(PurchaseOrder));

            using (var writer = new StringWriter())
            {
                serializer.Serialize(writer, order);

                Console.WriteLine(writer.ToString());
            }
        }
    }

    public class PurchaseOrder
    {
        [XmlElement("Item")]
        public Item[] ItemsOrders { get; set; }
    }

    public class Item
    {
        public string ItemID { get; set; }

        public decimal ItemPrice { get; set; }
    }
}

<?xml version="1.0" encoding="utf-16"?>
<PurchaseOrder xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <Item>
    <ItemID>1</ItemID>
    <ItemPrice>1723</ItemPrice>
  </Item>
  <Item>
    <ItemID>2</ItemID>
    <ItemPrice>4711</ItemPrice>
  </Item>
</PurchaseOrder>
Press any key to continue . . .

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