0

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.

5
  • Does the objects not serialized have the [Serializable] attribute? Commented Dec 18, 2012 at 21:45
  • 1
    If you haven't receive the answer you expect do not post another question. Try updating you previous one in a way somebody can answer it as expected. Commented Dec 18, 2012 at 21:47
  • @MattiasJosefsson Yes, they are serializable, but they are just <hero /> or <boss /> in the saved file. Commented Dec 18, 2012 at 21:48
  • @Fiire Do your Hero and Boss classes have public properties? Perhaps you can post one for us here. Commented Dec 18, 2012 at 21:59
  • @Fiire Thanks for posting the Hero class. Check the edit on my answer. Commented Dec 18, 2012 at 22:06

2 Answers 2

3

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.

Sign up to request clarification or add additional context in comments.

2 Comments

Silly me, it isn't enough to make the class public? I think that Image is actually serializable (msdn). EDIT: never mind. It can't be serialized.
@Fiire No. If you do not explicitly declare the visibility of a class's member, it will always default to private; it does not inherit the visibility of the class it's declared on.
2
XmlSerializer serializer = new XmlSerializer(typeof(SerialAssassin));
SerialAssassin assassin;

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.