Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Serializing object. Need Help.

Serializing object. Need Help.

Scheduled Pinned Locked Moved C#
xmlhelpquestionannouncement
2 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • Z Offline
    Z Offline
    zaboboa
    wrote on last edited by
    #1

    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.

    Z 1 Reply Last reply
    0
    • Z zaboboa

      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.

      Z Offline
      Z Offline
      Zoltan Balazs
      wrote on last edited by
      #2

      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

      1 Reply Last reply
      0
      Reply
      • Reply as topic
      Log in to reply
      • Oldest to Newest
      • Newest to Oldest
      • Most Votes


      • Login

      • Don't have an account? Register

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • World
      • Users
      • Groups