Serializing object. Need Help.
-
Hi! I have the following classes:
[XmlRoot("shoppingList")] public class ShoppingList { private ArrayList listShopping; public ShoppingList() { listShopping = new ArrayList(); } [XmlElement("item")] public Item[] Items { get { Item[] items = new Item[listShopping.Count]; listShopping.CopyTo(items); return items; } set { if (value == null) return; Item[] items = (Item[])value; listShopping.Clear(); foreach (Item item in items) listShopping.Add(item); } } public int AddItem(Item item) { return listShopping.Add(item); } } // Items in the shopping list public class Item { [XmlElement("name")] public string name; [XmlElement("price")] public double price; public Item() { } public Item(string Name, string Price) { name = Name; price = Convert.ToDouble(Price); } }
When I Serialize it I get the following XML back:<?xml version="1.0" encoding="utf-8"?><shoppingList xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"><item><name>eggs</name><price>1.49</price></item><item><name>bread</name><price>0.99</price></item></shoppingList>
As you can see from the definition of the ShoppingList class, I have an attribute [XmlElement] defined before the Items property, with the "items" name. So in my XML every element is . What can I do, so that when I serialize my ShoppingList object, each "item" element will have it's own name. So, one element will be "Table1", next will be "Table2", and so on. Any help greatly appreciated. Thank you very much. -
Hi! I have the following classes:
[XmlRoot("shoppingList")] public class ShoppingList { private ArrayList listShopping; public ShoppingList() { listShopping = new ArrayList(); } [XmlElement("item")] public Item[] Items { get { Item[] items = new Item[listShopping.Count]; listShopping.CopyTo(items); return items; } set { if (value == null) return; Item[] items = (Item[])value; listShopping.Clear(); foreach (Item item in items) listShopping.Add(item); } } public int AddItem(Item item) { return listShopping.Add(item); } } // Items in the shopping list public class Item { [XmlElement("name")] public string name; [XmlElement("price")] public double price; public Item() { } public Item(string Name, string Price) { name = Name; price = Convert.ToDouble(Price); } }
When I Serialize it I get the following XML back:<?xml version="1.0" encoding="utf-8"?><shoppingList xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"><item><name>eggs</name><price>1.49</price></item><item><name>bread</name><price>0.99</price></item></shoppingList>
As you can see from the definition of the ShoppingList class, I have an attribute [XmlElement] defined before the Items property, with the "items" name. So in my XML every element is . What can I do, so that when I serialize my ShoppingList object, each "item" element will have it's own name. So, one element will be "Table1", next will be "Table2", and so on. Any help greatly appreciated. Thank you very much.First of because the Items property returns an array you should use the XmlArray attribute class:
[XmlArray("Items")] public Item[] Items { // get, set }
To specify a name for each item use the XmlAttribute class like this:public class Item { [XmlAttribute("name")] public string name; ....
Work @ Network integrated solutions | Flickr | A practical use of the MVC pattern