0

I have a weird problem with serializing a list of a nested class in C#. The class is defined as

public class Settings : XmlSerializable
{
    private string mPath = "";
    public string Path
    {
        get { return mPath; }
        set { mPath = value; }
    }

    [XmlArrayItem("ChatEventsAndReactions")]
    public List<MessageEvent> Events;
}

public abstract class XmlSerializable
{
    public virtual void Save(string path)
    {
        StreamWriter w = new StreamWriter(path);
        XmlSerializer s = new XmlSerializer(this.GetType());
        s.Serialize(w, this);
        w.Close();
    }

    public virtual void Load(string path)
    {
        if (File.Exists(path))
        {
            StreamReader sr = new StreamReader(path);
            XmlTextReader xr = new XmlTextReader(sr);
            XmlSerializer xs = new XmlSerializer(this.GetType());
            object c;
            if (xs.CanDeserialize(xr))
            {
                c = xs.Deserialize(xr);
                Type t = this.GetType();
                PropertyInfo[] properties = t.GetProperties();
                foreach (PropertyInfo p in properties)
                {
                    p.SetValue(this, p.GetValue(c, null), null);
                }
            }
            xr.Close();
            sr.Close();

        }
    }
}

and the class MessageEvent is defined as

public class MessageEvent
{
    public MessageData Message = new MessageData();
    public MessageReaction Reaction = new MessageReaction();

    public override string ToString()
    {
        string tmp = this.Message.ToString() + " " + this.Reaction.ToString();
        return tmp;
    }
}

public class MessageReaction
{
    public string ReactionType;
    public string Parameter;

    public override string ToString()
    {
        string tmp = "Reaction: " + this.ReactionType + " " +
                     "Parameter: " + this.Parameter;
        return tmp;
    }
}

public class MessageData
{
    public string ChannelName;
    public string ChannelID;
    public string SenderName;
    public string MessageID;
    public string Text;

    public MessageData()
    { }

    public MessageData(string content)
    {
        this.ConvertFromString(content);
    }

    public void Clear()
    {
        this.ChannelName = "";
        this.ChannelID = "";
        this.MessageID = "";
        this.SenderName = "";
        this.Text = "";

    }

    public void ConvertFromString(string msg)
    {
        int HeaderEnd = msg.IndexOf("]") + 1;
        string Header = msg.Substring(0, HeaderEnd);
        string Text = msg.Substring(HeaderEnd);
        Header = Header.Replace("[", "");
        Header = Header.Replace("]", "");
        Header = Header.Replace("\",\"", "\"");
        Header = Header.Replace("\",", "\"");
        char[] Seperator = { '\"' };
        string[] message = Header.Split(Seperator);
        this.ChannelName = message[2];
        this.ChannelID = message[1];
        this.MessageID = message[4];
        this.SenderName = message[3];
        this.Text = Text;
    }

    public void ConvertFromObject(object o)
    {
        MessageData tmp = (MessageData)o;
        this.ChannelName = tmp.ChannelName;
        this.ChannelID = tmp.ChannelID;
        this.SenderName = tmp.SenderName;
        this.MessageID = tmp.MessageID;
        this.Text = tmp.Text;
    }

    public override string ToString()
    {
        return "ChannelID: " + this.ChannelID + " " +
               "CannelName: " + this.ChannelName + " " +
               "SenderName: " + this.SenderName + " " +
               "MessageID : " + this.MessageID + " " +
               "Message: " + this.Text;
    }
}

The weird problem with that is that it works perfectly when saving the data (resulting in a clear XML with all the data) but when I read from the XML file, I get a list with the correct number of items, but they are all empty ("" in every string). Path works fine

Thanks for working through this wall of text ;o)

1 Answer 1

1

public List<MessageEvent> Events; is a field, not a property. You need to repeat the same process for fields as you've done for properties.

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

1 Comment

wow, how could i miss that, thank you very much, its working perfectly now

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.