Page Index Toggle Pages: 1 Send TopicPrint
Normal Topic Serialization + Copying custom Appointment class items from one Timetable to another (Read 4030 times)
Aleksandar
YaBB Newbies
*
Offline


I Love MindFusion! I really
do! :)

Posts: 11
Location: Serbia
Joined: May 28th, 2017
Serialization + Copying custom Appointment class items from one Timetable to another
Jun 13th, 2017 at 9:11pm
Print Post  
Hello again.

#Q1: How to (Binary if possible, if not, XML example will of course do the job) serialize custom appointment class? Currently, I have this:
Code
Select All
using (Stream file = File.Open(fileName, FileMode.Create))
{
    BinaryFormatter formatter = new BinaryFormatter();
    formatter.Serialize(file, TerminsByIds);
} 


TerminsByIds is just a Dictionary<string, MyTermin>, and the exception is:
Code
Select All
Unhandled Exception: System.Runtime.Serialization.SerializationException: Type 'MindFusion.Scheduling.Appointment' in Assembly 'MindFusion.Scheduling.Wpf, Version=3.3.1.10055, Culture=neutral, PublicKeyToken=d12631580350466b' is not marked as serializable. 



#Q2: How to achieve this? (note: this was originaly the first question, but I solved it).
I came across some problems while copying a custom Appointment instance (MyTermin is my class' name) from one calendar to another.
I want to have N Calendars (actually only one which changes it's data) with regular TimeTable view, and one global TimeTable which will have columns goruped by resources. Regular calendars are used to add MyTermin both to that one regular calendar and to the global calendar.


Please answer as soon as possible. It would be wonderful if You could do it by 11AM tomorrow.
Many thanks,
Aleksandar
  
Back to top
 
IP Logged
 
Meppy
God Member
*****
Offline


MindFusion support

Posts: 1783
Joined: Jul 20th, 2005
Re: Serialization + Copying custom Appointment class items from one Timetable to another
Reply #1 - Jun 14th, 2017 at 7:59am
Print Post  
Hi,

The items are not marked as serializable. Instead, you can use the built-in serialization in a XML document. Here is how:

Code
Select All
using (Stream file = File.Open(fileName, FileMode.Create))
{
	var doc = new XmlDocument();
	var context = new XmlSerializationContext(calendar1.Schedule, doc);

	Schedule.RegisterItemClass(typeof(MyTermin), "mytermin", 1);

	var rootElement = doc.CreateElement("root");
	doc.AppendChild(rootElement);

	foreach (var item in calendar1.Schedule.Items)
	{
		var itemElement = doc.CreateElement("item");
		rootElement.AppendChild(itemElement);
		item.SaveTo(itemElement, context);
	}

	doc.Save(file);
} 


Note that you need to mark your class as serializable by calling Schedule.RegisterItemClass.

Alternatively, you can rely on binary serialization by using an auxiliary serializable class, which encapsulates only the relevant item properties. For example:

Code
Select All
[Serializable]
public class ItemState
{
	public ItemState(Item item)
	{
		HeaderText = item.HeaderText;
		StartTime = item.StartTime;
		EndTime = item.EndTime;
	}

	public string HeaderText
	{
		get;
		set;
	}

	public DateTime StartTime
	{
		get;
		set;
	}

	public DateTime EndTime
	{
		get;
		set;
	}
} 


An then:

Code
Select All
using (Stream file = File.Open(fileName, FileMode.Create))
{
	var formatter = new BinaryFormatter();
	foreach (var item in calendar1.Schedule.Items)
		formatter.Serialize(file, new ItemState(item));
} 


Let me know if this helps.

Regards,
Meppy
  
Back to top
 
IP Logged
 
Aleksandar
YaBB Newbies
*
Offline


I Love MindFusion! I really
do! :)

Posts: 11
Location: Serbia
Joined: May 28th, 2017
Re: Serialization + Copying custom Appointment class items from one Timetable to another
Reply #2 - Jun 14th, 2017 at 10:24am
Print Post  
Meppy wrote on Jun 14th, 2017 at 7:59am:
Hi,

The items are not marked as serializable. Instead, you can use the built-in serialization in a XML document. Here is how:
...


I will try this now, but I would like to get a deserialization code, since I am not familiar with XML (de)serialization.

Thank You very much!

Kind regards,
Aleksandar
  
Back to top
 
IP Logged
 
Meppy
God Member
*****
Offline


MindFusion support

Posts: 1783
Joined: Jul 20th, 2005
Re: Serialization + Copying custom Appointment class items from one Timetable to another
Reply #3 - Jun 14th, 2017 at 11:20am
Print Post  
Hi,

Here is a sample illustrating XML serialization and deserialization of custom items:

https://mindfusion.eu/_samples/_sample_WpfApplication20.zip

Regards,
Meppy
  
Back to top
 
IP Logged
 
Aleksandar
YaBB Newbies
*
Offline


I Love MindFusion! I really
do! :)

Posts: 11
Location: Serbia
Joined: May 28th, 2017
Re: Serialization + Copying custom Appointment class items from one Timetable to another
Reply #4 - Jun 14th, 2017 at 12:37pm
Print Post  
Meppy wrote on Jun 14th, 2017 at 11:20am:
Here is a sample illustrating XML serialization and deserialization of custom items:
...


Works like a charm! Thank You very very much, sir!

Best wishes,
Aleksandar
  
Back to top
 
IP Logged
 
Page Index Toggle Pages: 1
Send TopicPrint