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

How to write an XMl

Discussion in 'Getting Started' started by megablast2, Aug 18, 2016.

  1. megablast2

    megablast2

    Joined:
    Aug 15, 2013
    Posts:
    6
    Hello,

    I want serialize a xml but don't know write for "size" of MonsterCollection and "test" of Health. Can you help me please?
    Code (CSharp):
    1.  
    2. <MonsterCollection size=10>
    3.     <Monsters>
    4.        <Monster name="a">
    5.          <Health test="1">5</Health>
    6.        </Monster>
    7. </MonsterCollection>
    8.  
    Code (CSharp):
    1. using System.Xml;
    2. using System.Xml.Serialization;
    3. public class Monster
    4. {
    5.     [XmlAttribute("name")]
    6.     public string Name;
    7.  
    8.    //test?
    9.  
    10.     public int Health;
    11.  
    12. }
    13.  
    Good Day,
     
    Last edited: Aug 18, 2016
  2. jhocking

    jhocking

    Joined:
    Nov 21, 2009
    Posts:
    813
    What does that mean?
     
  3. Gnatty

    Gnatty

    Joined:
    May 17, 2012
    Posts:
    77
    I'm guessing you want an attribute for the root element?
    Maybe this?
    (not tested)

    Code (CSharp):
    1. using System.Collections.Generic;
    2. using System.Xml;
    3. [XmlRoot("MonsterCollection")]
    4. public class MonsterContainer
    5. {
    6.      [XmlAttribute("size")]
    7.      public int size;
    8.  
    9.      [XmlArray("Monsters"),XmlArrayItem("Monster")]
    10.      public List<Monster> Monsters = new List<Monster>();
    11. }
     
  4. megablast2

    megablast2

    Joined:
    Aug 15, 2013
    Posts:
    6
    How do I put to have "test" of health and "size" of MonsterCollection?
     
  5. megablast2

    megablast2

    Joined:
    Aug 15, 2013
    Posts:
    6
    Sorry Gnatty, I test your code. Thank you.