Nayan
Nayan

Reputation: 327

XMLSerialization for a List

I have class like this below shown. which contains the shopping items where the number can vary from 0 to n.

namespace SerializationPOC
{

   public class ShoppingItems
   {
     public string CustomerName { get; set; }
     public string Address { get; set; }
     public List<Item> Items { get; set; }
   }

   public class Item
   {
    public string Name { get; set; }
    public string Price { get; set; }
   }
}

Is it possible to get the class serialized like to get the XML Schema like below.

<?xml version="1.0" encoding="utf-8" ?>
<ShoppingItems>
<CustomerName>John</CustomerName>
<Address>Walstreet,Newyork</Address>
<Item1>Milk</Item1>
<Price1>1$</Price1>
<Item2>IceCream</Item2>
<Price2>1$</Price2>
<Item3>Bread</Item3>
<Price3>1$</Price3>
<Item4>Egg</Item4>
<Price4>1$</Price4>


<Item..n>Egg</Item..n>
<Price..n>1$</Price..n>
</ShoppingItems>

I would like to know if this can be achieved by using the Serilization if not whats the best way to achieve this Schema?

Upvotes: 1

Views: 159

Answers (2)

Md. Rashim Uddin
Md. Rashim Uddin

Reputation: 502

Can you please have a look on my article, [^]

As an example you can look into the below code. The Serialize method is given on the article.

var test = new ShoppingItems()
                           {
                                CustomerName = "test",
                                 Address = "testAddress",
                                  Items = new List<Item>()
                                              {
                                                  new Item(){ Name = "item1", Price = "12"},
                                                  new Item(){Name = "item2",Price = "14"}
                                              },
                           };

            var xmlData = Serialize(test);

And it will return the string given below,

<?xml version="1.0" encoding="utf-16"?>
 <ShoppingItems xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
    <CustomerName>test</CustomerName>
    <Address>testAddress</Address>
    <Items>
        <Item>
            <Name>item1</Name>
            <Price>12</Price>
       </Item>
       <Item>
           <Name>item2</Name>
           <Price>14</Price>
       </Item>
   </Items>
</ShoppingItems>

Upvotes: 0

Marc Gravell
Marc Gravell

Reputation: 1064104

There is no standard serializer that supports that layout. You will have to do it yourself. Personally, I would say "you're doing it wrong"; I strongly suggest (if it is possible) using a format like

<Item name="IceCream" Price="1$"/>

or

<Item><Name>IceCream</Name><Price>1$</Price></Item>

both of which would be trivial with XmlSerializer.

LINQ-to-XML is probably your best option, something like:

var items = new ShoppingItems
{
    Address = "Walstreet,Newyork",
    CustomerName = "John",
    Items = new List<Item>
    {
        new Item { Name = "Milk", Price = "1$"},
        new Item { Name = "IceCream", Price = "1$"},
        new Item { Name = "Bread", Price = "1$"},
        new Item { Name = "Egg", Price = "1$"}
    }
};

var xml = new XElement("ShoppingItems",
    new XElement("CustomerName", items.CustomerName),
    new XElement("Address", items.Address),
    items.Items.Select((item,i)=>
        new[] {
            new XElement("Item" + (i + 1), item.Name),
            new XElement("Price" + (i + 1), item.Price)}))
    .ToString();

Upvotes: 4

Related Questions