Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7874551
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T02:49:40+00:00 2026-06-03T02:49:40+00:00

I have a weird problem with serializing a list of a nested class in

  • 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 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-03T02:49:40+00:00Added an answer on June 3, 2026 at 2:49 am

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a weird problem in converting a string to double in .NET 3.5.
I have a weird problem: I extended Zend_Log with a class called \Application\Log (it
I have a weird problem with datagrid. I have a list with 2 items,
I have a weird problem. I have checking a php string like this: On
I have a weird problem with PHP4 I have a class which assigns a
I have a weird problem, I need to parse a date string that looks
i have a weird problem. Basically, in my settings.py file i have 4 variables
I have a weird problem using the Java Box class. I am using JDK
I have a weird problem... I use this code: String text = new String(values[0]);
I have a weird problem where after setting nocheck on a foreign constraint and

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.