Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

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:

Anon - "If you can't beat your computer at chess, try kickboxing."

 

homepage: http://www.givemehelp.co.uk/

  • 2 months later...
Posted

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).

 

:)

  • *Experts*
Posted

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.

"Being grown up isn't half as fun as growing up

These are the best days of our lives"

-The Ataris, In This Diary

  • 1 month later...
Posted
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.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...