2

i get an empty xmlfile after serializing an object. I'm using Monodevelop and Unity 4. I searched for such a long time mostly in this community, but i only found difficult problems with even more difficult answers :) I think mine is so simple, please help me. (I am new to c#) The serialized object is this:

[System.Serializable]
public class information  {
    private string data1;
    private string data2;
    private string data3;

public void Data1(string text)
{
        data1 = text;
}
public string GetData1 ()
{
    return data1;   
}
    public void Data2(string text)
    {
        data2 = text;
    }
public string GetData2 ()
{
    return data2;   
}
    public void Data3(string text)
    {
        data3 = text;
    }
}

the serializing class is this, here might be the problem:

public class SaveXml {
        public void SaveData(object obj, string filename)
{
        XmlSerializer sr = new XmlSerializer(obj.GetType());    
        TextWriter writer = new StreamWriter(filename);
        sr.Serialize(writer, obj);
        writer.Close();
    }

    public string Load()
{
    if(File.Exists("accdata.xml"))
    {
        XmlSerializer xs = new XmlSerializer(typeof(information));
        FileStream read = new FileStream("accdata.xml",FileMode.Open, FileAccess.Read, FileShare.Read);
        information info = (information)xs.Deserialize(read);
        return info.GetData1();
    }
    else
    {
        return "file does not exist";
    }
}

And the serializing and the serialized object get called by a menu that has this 2 buttons:

if(GUI.Button(new Rect(10,50,300,100),"Save"))
{
    SaveXml saver = new SaveXml();
    information infol = new information();
    infol.Data1("textone");
    infol.Data2("texttwo");
    infol.Data3( "textthree");
    saver.SaveData(infol, "accdata.xml");
}   
if(GUI.Button(new Rect(500,50,300,100),"Load"))
{
    SaveXml saver1 = new SaveXml();
    text = saver1.Load();
}

so the variable text that is declared in the class menu, should be "textone", after i clicked the Save Button and the LoadButton. The Savebutton creates a file that is empty.

The Deserialization seems to work but of course there is no String in the data1 variable in Information so the variable in the menu called text is empty too. I get no errors and i can work with the object after serialization. So why doesnt my serialization work? Please help me. I excuse for my bad english and mistakes, i am new to stackoverflow.

Yuck
  • 49,664
  • 13
  • 105
  • 135
kili vettori
  • 23
  • 1
  • 3

3 Answers3

4

Xml serializer serializes public fields/properties not methods. Change your methods to properties. For ex,

    public string Data2
    {
        set { data2 = value; }
        get { return data2; }
    }

So your information class can be

public class Information
{
    public string Data1 { get; set; }
    public string Data2 { get; set; }
    public string Data3 { get; set; }
}

BTW: you don't need this Serializable attribute. It is only used by BinaryFormatter

I4V
  • 34,891
  • 6
  • 67
  • 79
  • OMG, thank you so much!! I never thought your answers would be that fast, you wouldn't believe how happy i am. :) sadly i need reputation to give you a thumbsup :( It works but could you explain sth to me: My methods changed the variables, thats why i thought it would be the same. Where is the difference between your get and set thing and my methods? – kili vettori Aug 10 '13 at 21:24
  • @kilivettori http://www.codersource.net/MicrosoftNet/CBasicsTutorials/CNetmethodsandproperties.aspx – I4V Aug 10 '13 at 22:04
2

I'm not sure but from what i see you don't have any public fields... Take a look here

And also, why don't you just use auto getter/setter ?

Community
  • 1
  • 1
Emmanuel Istace
  • 1,209
  • 2
  • 14
  • 32
0

According to this MSDN support article, using XmlSerializer the way you have performs only "shallow" serialization - it only serializes public fields/properties. To serialize private data requires "deep" serialization which appears to be a whole other animal.

Zenilogix
  • 1,318
  • 1
  • 15
  • 31