473,323 Members | 1,537 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,323 software developers and data experts.

OptionalField attribute doesn't seem to work

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.

All I'm trying to do is add fields to objects which have already been
serialized. For example, I have serialized this object (and
deserialized successfully):

[Serializable()]
class Person
{
private string _FirstName = "";
private string _LastName = "";

... Public Properties omitted
}

I then added a field marked with the OptionalField attribute

[Serializable()]
class Person
{
private string _FirstName = "";
private string _LastName = "";

[System.Runtime.Serialization.OptionalField]
private string _Title = "";

... Public Properties omitted
}

I then got the old "Wrong number of Members. Object SqlXMLTest.Person
has 5 members, number of members deserialized is 4." exception.

I thought that the OptionalField attribute was designed to solve this
exact problem. Has anyone seen this?

BTW - I'm using the SOAPFormatter.

Thanks,
-Rich

Jun 19 '06 #1
4 5955
<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
Jun 19 '06 #2
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


Jun 19 '06 #3
<ri**************@gmail.com> wrote:
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.


<snip>

Right. Thanks for the code. It fails for me too. Interestingly, it
works with the binary formatter.

I suggest you report it as a bug (with the complete code below) at
http://lab.msdn.microsoft.com/produc...k/default.aspx

It's worth giving all the information you've found out about when it
fails and when it works.

--
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
Jun 20 '06 #4
Thanks Jon - I submitted the item to Microsoft.

For anyone that is interested:
http://connect.microsoft.com/VisualS...dbackID=140592

-Rich

Jon wrote:
Right. Thanks for the code. It fails for me too. Interestingly, it
works with the binary formatter.

I suggest you report it as a bug (with the complete code below) at
http://lab.msdn.microsoft.com/produc...k/default.aspx

It's worth giving all the information you've found out about when it
fails and when it works.

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


Jun 20 '06 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

39
by: Mark Johnson | last post by:
It doesn't seem possible. But would the following also seem a violation of the general notions behind css? You have a DIV, say asociated with class, 'topdiv'. Inside of that you have an anchor...
8
by: Mattias Campe | last post by:
Hello, On http://student.ugent.be/astrid/bewoners.php I got the problem that I want Javascript to let my browser go to http://student.ugent.be/astrid/bewoners.php?beginAcjaar=2002 when I select...
3
by: Matt | last post by:
I want to know if readOnly attribute doesn't work for drop down list? If I try disabled attribute, it works fine for drop down list. When I try text box, it works fine for both disabled and...
10
by: Brett | last post by:
This code is supposed to work in Netscape 4+ and IE 4+. It works fine in IE but in Netscape 7.2, I get a blank page. Any suggestions? Thanks, Brett <html> <head>
4
by: dot | last post by:
I'm trying to use the following hack to create an expanding/contracting list of links: <html> <head> <script language="Javascript" type="text/javascript"> function ChangeText(obj,...
2
by: COHENMARVIN | last post by:
Folks, I wrote a simple javascript script, but it doesn't work. First I have a web page that uses frames: ===================== <html> <frameset rows="10%,90%"> <frame src="frame1.htm"...
1
by: LaserMan | last post by:
I'm trying to figure out how to specify the equivalent of the C# out attribute, and apparently is supposed to do the trick, but it only seems to work with interfaces. Any ideas on how to make...
1
by: Samuel | last post by:
The MaxLenght property doesn't seem to have any effect
1
by: Maria Carlson | last post by:
My HTML calls a JavaScript on-click to open a new window. My id attribute doesn't work when I use a hyphen but it does work when I replace it with an underscore. <script language="JavaScript"...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.