Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

XML Serialization: Setting up object arrays

Discussion in 'Scripting' started by Arktor314, Jul 18, 2014.

  1. Arktor314

    Arktor314

    Joined:
    Aug 20, 2012
    Posts:
    28
    Hey all,

    I've been following the XMLSerializer tutorial, and I was wondering if there's a way to condense the XML Array so that instead of defining both an XmlArray and XmlArrayItem, you could just define the XmlArrayItem.
    Here's the tutorial array:
    Code (csharp):
    1.  <MonsterCollection>
    2.      <Monsters>
    3.          <Monster name="a">
    4.              <Health>5</Health>
    5.          </Monster>
    6.          <Monster name="b">
    7.              <Health>3</Health>
    8.          </Monster>
    9.      </Monsters>
    10. </MonsterCollection>
    and corresponding code:
    Code (CSharp):
    1. [XmlRoot("MonsterCollection")]
    2. public class MonsterContainer
    3. {
    4. [XmlArray("Monsters")]
    5. [XmlArrayItem("Monster")]
    6. public List<Monster> Monsters = new List<Monster>();
    7. }

    Is it possible to remove the "<Monsters>" and "</Monsters>" elements, and just directly list the <Monster> elements? How would you go about doing that?

    I'd like to store information slightly more succinctly, like:

    Code (csharp):
    1. <MonsterCollection>
    2.      <Monster name="a" />
    3.      <Monster name="b" />
    4. </MonsterCollection>
     
  2. angrypenguin

    angrypenguin

    Joined:
    Dec 29, 2011
    Posts:
    15,500
    What's the purpose?

    The XML is mirroring exactly what your data structure is. You've got a class that contains a List that contains items. If you want the class to contain the items directly then you'd need to take away the List, which would mean using a flat array instead.

    The XML explicitly stores the properties of each object. The List "Monsters" is an object, so if it weren't there it wouldn't be describing exactly the data you have.
     
  3. Arktor314

    Arktor314

    Joined:
    Aug 20, 2012
    Posts:
    28
    Just trying to make my XML file a little cleaner. It's not an issue with this example, but in my file I expect to end up nesting 2-3 layers.


    That's a good point. Does XmlSerializer not have a feature to implicitly determine array elements?
     
  4. angrypenguin

    angrypenguin

    Joined:
    Dec 29, 2011
    Posts:
    15,500
    Actually, I think I was wrong when I mentioned a flat array, because that's still a "collection", and I think all types of collection are handled similarly.

    What do you mean "implicitly determine array elements"? What you see in the XML is literally all of the information available to re-create the entire data set you're persisting. You want it to imply as little as possible.

    For what it's worth, the intention of the XMLSerializer isn't, to my understanding, that the XML is "clean" or otherwise nice for humans to use. It's about being portable and platform independent. Think about the number of ways that even primitive numbers can be stored in memory and you'll immediately see why flattening everything to text and re-interpreting that natively at the other end can be handy.
     
  5. billykater

    billykater

    Joined:
    Mar 12, 2011
    Posts:
    327
  6. Arktor314

    Arktor314

    Joined:
    Aug 20, 2012
    Posts:
    28