2012-04-29 94 views
0

我有一個奇怪的問題,在C#中序列化一個嵌套類的列表。該類被定義爲XML序列化一個包含列表的嵌套類

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(); 

     } 
    } 
} 

和類MessageEvent被定義爲

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; 
    } 
} 

怪異的問題,那就是它在保存數據(以產生清澈的XML與所有的數據時,完美的作品),但是當我從XML文件中讀取時,我得到一個正確數量的項目列表,但它們都是空的(每個字符串中有"")。路徑工作正常

感謝這堵牆文本的工作; O)

回答

1

public List<MessageEvent> Events;是一個領域,而不是財產。您需要爲字段重複相同的過程,就像您爲屬性所做的那樣。

+0

哇,我怎麼可能會想念,非常感謝,現在完美的工作 – Aseratis 2012-04-29 09:04:16