OFRBG
OFRBG

Reputation: 1778

Load object data from XML in C#

I have the following class:

[Serializable]

public class SerialAssassin
{
    public Hero hero;
    public Point heroPB;
    public Boss boss;
    public Point bossPB;
    public Attack attack;
    public Point attackPB;
    public HPMeter bossHP;
    public Point bossHPPB;
    public PPMeter heroPP;
    public Point heroPPPB;

    public Rectangle bossRect;
    public Rectangle attackRect;

    public int heroState;
    public int stepRate;
    public int attackDirection;
    public int attackLoop;
    public int contadorPaso;
    public int contadorPasoBoss;
    public int bossTop, bossLeft;
    public int bossState;
    public int bossHealth;
    public int bossHPCap;
    public int opa;
    public int battlesWon;
    public int mainBossCounter;
    public int ppleft;
    public bool paso;
    public bool inStadium;
    public bool fading;
    public bool fightingMainBoss;
    public bool fainted;
    public string currentPokemon;
}

I'm having problems reading the data from the XML, which was written as follows:

XmlSerializer serializer = new XmlSerializer(typeof(SerialAssassin));
TextWriter textWriter = new StreamWriter(@"..\..\Resources\saveState.xml");
serializer.Serialize(textWriter, serial);
textWriter.Close();

From there, I don't quite know how to read the data. Plus the fact that the XML doesn't serialize the objects of Hero, Boss, Attack, HPMeter, PPMeter.

Hero class:

public class Hero
    {

        int state = 0;
        int x, y;
        string path;
        Image img;


        //methods
    }

I'd be grateful if you would be so kind as to explain to me how to load those objects/primitives and then use them.

Upvotes: 0

Views: 6655

Answers (2)

Chris Sinclair
Chris Sinclair

Reputation: 23208

IIRC, the XmlSerializer checks for properties, not fields. (I think it can use public fields, but you really ought to switch to properties anyway) In addition, classes do not need to be marked as Serializable. (Serializable is used for others such as binary and SOAP serializers)

Replace your fields with properties with public getters and setters. In addition, make sure your other classes (such as Hero, Point, Boss) are all also serializable according to XmlSerializer's rules:

public class SerialAssassin
{
    public Hero hero { get; set; }
    public Point heroPB { get; set; }
    public Boss boss { get; set; }
    public int heroState { get; set; }
    ...

To deserialize, use its Deserialize method (http://msdn.microsoft.com/en-us/library/system.xml.serialization.xmlserializer.deserialize.aspx):

Stream xmlInputStream = ... //get your file stream, or TextReader, or XmlReader
XmlSerializer deserializer = new XmlSerializer(typeof(SerialAssassin));
SerialAssassin assassin = (SerialAssassin)deserializer.Deserialize(xmlInputStream)

EDIT: Looking at your sample Hero class, it's not serializing any of its values because you have declared them all to be private. Make them public instead.

public class Hero
{
    public int state {get; set; }
    public int x { get; set; }
    public int y { get; set; }
    public string path { get; set; }

    [XmlIgnore]
    public Image img { get; set; }
}

I suspect that Image will not be serializable, so you may want to store the image's file path (or some other identifying information) so you can save/load it. [XmlIgnore] will instruct the XmlSerializer to ignore that property so it doesn't fail during serialization/deserialization.

Upvotes: 3

Sergey Berezovskiy
Sergey Berezovskiy

Reputation: 236228

XmlSerializer serializer = new XmlSerializer(typeof(SerialAssassin));
SerialAssassin assassin;

using(var reader = File.OpenText(@"..\..\Resources\saveState.xml"))
{
   assassin = (SerialAssassin)serializer.Deserialize(reader);
}

Upvotes: 2

Related Questions