I want to serialize a class that I am using to retain some information the
user types into a screen. I have 3 questions.
1) I serialized it as XML to start with. This works, but how do I serialize
the strings so that they are not messed up if they have XML in them, or
control characters? Is there a way to do that in XML, or do I have to use
BinaryFormatters?
2) So I tried using a binary formatter, and it won't serialize/deserialize
the darn thing. It doesn't look like rocket science, but apparently I'm
missing something. Simplified example below.
3) If I use a binary formatter (or an XML one for that matter), what
happens if/when I add a new property to my class? Do I need to account for
that somehow?
I'm posting all of the code with the three test cases (one uses a memory
stream just for grins; it doesn't work either). It's about 130 lines of
code. Hope this isn't too much to post. Hopefully the intendation will come
across right.
//================================================== =================
using System;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;
using System.Xml.Serialization;
using System.IO;
namespace TestFruitLoopsTiny
{
[Serializable()]
public class BusinessCardData
{
[NonSerialized()]
private string _NameData;
public string NameData
{
get { return _NameData; }
set { _NameData = value; }
}
[NonSerialized()]
private string _CompanyData;
public string CompanyData
{
get { return _CompanyData; }
set { _CompanyData = value; }
}
public BusinessCardData()
{
SetDefaults();
}
public void SetDefaults()
{
this.NameData = string.Empty;
this.CompanyData = string.Empty;
}
public void TestBinary()
{
//Write it
using (FileStream fs
= new FileStream("D:\\test.dat", FileMode.Create))
{
BinaryFormatter bf = new BinaryFormatter(null,
new StreamingContext(StreamingContextStates.File));
bf.Serialize(fs, this);
OutputData("Original data (binary)", this);
}
//Read it
if (System.IO.File.Exists("D:\\test.dat"))
{
using (FileStream fs
= new FileStream("D:\\test.dat", FileMode.Open))
{
BinaryFormatter bf = new BinaryFormatter(null,
new StreamingContext(StreamingContextStates.File));
BusinessCardData bcd = (BusinessCardData)bf.Deserialize(fs);
OutputData("After reading it back in (binary) ", bcd);
}
}
}
public void TestXML()
{
XmlSerializer xs = new XmlSerializer(typeof(BusinessCardData));
using (FileStream fs = new FileStream("D:\\test.dat",
FileMode.Create))
{
xs.Serialize(fs, this);
OutputData("Original data (XML)", this);
}
//read it back in
BusinessCardData bcd;
using (FileStream fs = new FileStream("D:\\test.dat", FileMode.Open))
{
bcd = (BusinessCardData)xs.Deserialize(fs);
}
OutputData("After reading it back in (XML)", bcd);
}
public void TestMemory()
{
using (MemoryStream ms = new MemoryStream(200))
{
OutputData("OriginalData (memory)", this);
BinaryFormatter bf = new BinaryFormatter(null,
new StreamingContext(StreamingContextStates.Clone));
bf.Serialize(ms, this);
//read it back in
ms.Seek(0, SeekOrigin.Begin);
BusinessCardData abc = (BusinessCardData)bf.Deserialize(ms);
OutputData("After reading it back in (memory)", abc);
}
}
private static void OutputData(string caption,
BusinessCardData bcd)
{
Console.WriteLine(caption);
Console.WriteLine(" Name = {0}, Company = {1}",
bcd.NameData, bcd.CompanyData);
}
}
}
//================================================== =================
//****TEST ROUTINE***
static void Test()
{
BusinessCardData bcd = new BusinessCardData();
bcd.CompanyData = "Apple";
bcd.NameData = "Robin";
bcd.TestBinary();
bcd.CompanyData = "Apple";
bcd.NameData = "Robin";
bcd.TestXML();
bcd.CompanyData = "Apple";
bcd.NameData = "Robin";
bcd.TestMemory();
Console.ReadLine("Press any key to return");
}
//================================================== =================
I'm sure I'm doing something stupid, or stupidly NOT doing something.
I'd appreciate any help you can provide.
Thanks,
Robin S.