kingelessar Posted July 14, 2003 Posted July 14, 2003 Hey guys, I am busy creating a Log program in C#. I want to log files to be saved in Xml. I want to do this with Xml Serialization. This is my LogFile class: [XmlRoot("logfile")] public class LogFile { [XmlElement("title",typeof(string))] public string LogTitle; [XmlArray("entries")] [XmlArrayItem("entry",typeof(LogEntry))] public ArrayList Entries; public LogFile() { Entries = new ArrayList(); } public void WriteEntry(string user, string text) { Entries.Add(new LogEntry(user,text)); } } public class LogEntry { [XmlAttribute("dateandtime",typeof(DateTime))] public DateTime DateAndTime; [XmlAttribute("user",typeof(string))] public string User; [XmlText(typeof(string))] public string Text; public LogEntry(string user, string text) { DateAndTime = DateTime.Now; User = user; Text = text; } } This is my (de)serialization code: public void Serialize() { XmlSerializer s = new XmlSerializer(typeof(LogFile)); TextWriter writer = new StreamWriter(file); s.Serialize(writer, mylog); writer.Close(); } public void DeSerialize() { FileStream fs = new FileStream(file, FileMode.Open); XmlSerializer x = new XmlSerializer(typeof(LogFile)); mylog = (LogFile)x.Deserialize(fs); } When I make a new LogFile mylog object and add some entries (mylog.WriteEntry("John Doe","Logged on!")), and set the log file title (mylog.LogTitle = "My Log") and compile it and run it, it gives the following error: An unhandled exception of type 'System.InvalidOperationException' occurred in system.xml.dll Additional information: There was an error reflecting 'XmlSerialization.LogFile'. I really don't understand why it doesn't work. Please help! Thanx :confused: Quote Anon - "If you can't beat your computer at chess, try kickboxing." homepage: http://www.givemehelp.co.uk/
kingelessar Posted July 14, 2003 Author Posted July 14, 2003 I solved it! I searched on google and found an example of Xml Serialization. I found out that i had to add an empty constructor to the classes i wanted to serialize. Thanx :rolleyes: Quote Anon - "If you can't beat your computer at chess, try kickboxing." homepage: http://www.givemehelp.co.uk/
Soon Posted September 14, 2003 Posted September 14, 2003 I have the same problem, so can you post the url please? Thanx :rolleyes: Quote
kingelessar Posted September 20, 2003 Author Posted September 20, 2003 I don't know the exact uri i used because its a long long time ago ;) . Here is a good website: http://www.developerfusion.com/show/3827/ Dennis Quote Anon - "If you can't beat your computer at chess, try kickboxing." homepage: http://www.givemehelp.co.uk/
Soon Posted September 20, 2003 Posted September 20, 2003 Xml Serializer Thanks, But, what i would like to do is to serialize a classe that can contain an Arraylist or an Hashtable Object. The Hashtable may contain object types more complex than string (recursive serialization). :) Quote
*Experts* Bucky Posted September 21, 2003 *Experts* Posted September 21, 2003 Both the ArrayList and HashTable classes are marked with the Serializable attribute, so as long as the classes in the collections are also Serializable you shuld be all set. Quote "Being grown up isn't half as fun as growing up These are the best days of our lives" -The Ataris, In This Diary
mogwai Posted October 27, 2003 Posted October 27, 2003 Both the ArrayList and HashTable classes are marked with the Serializable attribute, so as long as the classes in the collections are also Serializable you shuld be all set. This is not the case when using XML serialization however. The XmlSerializer cannot serialize Hashtables, or any class that implements the IDictionary interface. Quote
kingelessar Posted October 27, 2003 Author Posted October 27, 2003 ehm for Xml serialization I never use the serializable attribute and everything works fine with simple classes, as long as i have an empty constructor Thanx for ur responses. Dennis Quote Anon - "If you can't beat your computer at chess, try kickboxing." homepage: http://www.givemehelp.co.uk/
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.