Sorry about that... You are absolutely right.
I've also gotten closer to the issue... This issue appears when trying
to desserialize an object with a Guid field AND an OptionalField.
The below code should work by pasting over the class creating when
making a Console Application.
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Soap;
namespace ConsoleApplication1
{
using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Soap;
[Serializable()]
class Person
{
//Changing this to a string gets rid of the error
private Guid _PersonID;
private string _FirstName = "";
private string _LastName = "";
//Save the object, then uncomment these two lines and
//[OptionalField]
//private string _Title = "";
public string FirstName
{
get { return _FirstName; }
set { _FirstName = value; }
}
public string LastName
{
get { return _LastName; }
set { _LastName = value; }
}
public void Save()
{
System.IO.FileStream oStream = new
System.IO.FileStream("c:\\Person.xml",
System.IO.FileMode.Create);
IFormatter oFormatter = (IFormatter)new SoapFormatter();
oFormatter.Serialize(oStream, this);
oStream.Close();
}
public static Person LoadPerson()
{
System.IO.FileStream oStream = new
System.IO.FileStream("c:\\Person.xml",
System.IO.FileMode.Open);
IFormatter oFormatter = (IFormatter)new SoapFormatter();
//After adding a field (e.g. _Title) and there is a Guid
field,
//the serialization exception occurs here
Person oPerson = (Person)oFormatter.Deserialize(oStream);
oStream.Close();
return oPerson;
}
}
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Press S to save a person, L to load a
person");
ConsoleKeyInfo oKeyInfo = Console.ReadKey();
if (oKeyInfo.Key == ConsoleKey.S)
{
Person oPerson = new Person();
oPerson.FirstName = "Kevin";
oPerson.LastName = "Jamison";
oPerson.Save();
Console.WriteLine("Person saved.. Press any key to
exit");
Console.ReadKey();
}
else if (oKeyInfo.Key == ConsoleKey.L)
{
Person oPerson = Person.LoadPerson();
Console.WriteLine("Person loaded \"{0}\"... \nPress any
key to exit",
oPerson.FirstName);
Console.ReadKey();
}
else
{
Console.WriteLine("Invalid key.. Press any key to
exit");
Console.ReadKey();
}
}
}
}
Why would the presence of a Guid type make a difference in
versionability.
Jon wrote:
<ri**************@gmail.com> wrote: I was very excited when version 2.0 of the framework came out... it
looked as though the OptionalField atribute would help me sort out my
serialization woes. So far, it hasn't through.
Could you post a short but complete program which demonstrates the
problem?
See http://www.pobox.com/~skeet/csharp/complete.html for details of
what I mean by that.
--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too