Serialize Class To XML

D

Dan

All I Am Attempting To Serialize An Object To An XML File.
Here Is The Code For That

public string SaveNewSurvey( MutualSurveyObject
mso_TempObject, int i_JobID )
{
string s_RootFileName;
string s_FinalFileName;

try
{

//Create The Final File Name
s_RootFileName = "job" + i_JobID.ToString() + "config.xml";
s_FinalFileName =
s_SetUpFilePath + @"\" + s_RootFileName;

//Serialize The Object And Write The XML Data
TextWriter tr = new StreamWriter( s_FinalFileName );
XmlSerializer sr = new XmlSerializer( typeof(
MutualSurveyObject ) );
sr.Serialize( tr, mso_TempObject );
tr.Close();

return s_RootFileName;

}

catch ( Exception e )
{
throw e;
}
}

However, When I attempt to execute this I get the
following rather long Exception message

System.Runtime.Serialization.SerializationException: The
type FNIS.MutualSurveyApplication.MutualSurveyObject in
Assembly MutualSurveyStructureObject,
Version=1.0.1411.14253, Culture=neutral,
PublicKeyToken=null is not marked as serializable.

Server stack trace:
at
System.Runtime.Serialization.FormatterServices.InternalGetS
erializableMembers(RuntimeType type, Boolean
excludeNonSerializable)
at
System.Runtime.Serialization.FormatterServices.GetSerializa
bleMembers(Type type, StreamingContext context)
at
System.Runtime.Serialization.Formatters.Binary.WriteObjectI
nfo.InitMemberInfo()
at
System.Runtime.Serialization.Formatters.Binary.WriteObjectI
nfo.InitSerialize(Object obj, ISurrogateSelector
surrogateSelector, StreamingContext context,
SerObjectInfoInit serObjectInfoInit, IFormatterConverter
converter)
at
System.Runtime.Serialization.Formatters.Binary.WriteObjectI
nfo.Serialize(Object obj, ISurrogateSelector
surrogateSelector, StreamingContext context,
SerObjectInfoInit serObjectInfoInit, IFormatterConverter
converter)
at
System.Runtime.Serialization.Formatters.Binary.ObjectWriter
..Serialize(Object graph, Header[] inHeaders,
__BinaryWriter serWriter, Boolean fCheck)
at
System.Runtime.Serialization.Formatters.Binary.BinaryFormat
ter.Serialize(Stream serializationStream, Object graph,
Header[] headers, Boolean fCheck)
at
System.Runtime.Serialization.Formatters.Binary.BinaryFormat
ter.Serialize(Stream serializationStream, Object graph,
Header[] headers)
at
System.Runtime.Remoting.Channels.CoreChannel.SerializeBinar
yMessage(IMessage msg, Stream outputStream, Boolean
includeVersions)
at
System.Runtime.Remoting.Channels.BinaryClientFormatterSink.
SerializeMessage(IMessage msg, ITransportHeaders& headers,
Stream& stream)
at
System.Runtime.Remoting.Channels.BinaryClientFormatterSink.
SyncProcessMessage(IMessage msg)


The object is complete in terms of being one object that
holds collections of other objects. Is that the problem?
I am lost. Any help would be appreciated.

Dan
 
N

Nicholas Paldino [.NET/C# MVP]

Dan,

Are you sure you are using an XML serializer here? The stack trace you
provide hints that binary serialization is being used somewhere, in which
case, you need to make sure that a type and all the types it references are
marked as serializable.

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Dan said:
All I Am Attempting To Serialize An Object To An XML File.
Here Is The Code For That

public string SaveNewSurvey( MutualSurveyObject
mso_TempObject, int i_JobID )
{
string s_RootFileName;
string s_FinalFileName;

try
{

//Create The Final File Name
s_RootFileName = "job" + i_JobID.ToString() + "config.xml";
s_FinalFileName =
s_SetUpFilePath + @"\" + s_RootFileName;

//Serialize The Object And Write The XML Data
TextWriter tr = new StreamWriter( s_FinalFileName );
XmlSerializer sr = new XmlSerializer( typeof(
MutualSurveyObject ) );
sr.Serialize( tr, mso_TempObject );
tr.Close();

return s_RootFileName;

}

catch ( Exception e )
{
throw e;
}
}

However, When I attempt to execute this I get the
following rather long Exception message

System.Runtime.Serialization.SerializationException: The
type FNIS.MutualSurveyApplication.MutualSurveyObject in
Assembly MutualSurveyStructureObject,
Version=1.0.1411.14253, Culture=neutral,
PublicKeyToken=null is not marked as serializable.

Server stack trace:
at
System.Runtime.Serialization.FormatterServices.InternalGetS
erializableMembers(RuntimeType type, Boolean
excludeNonSerializable)
at
System.Runtime.Serialization.FormatterServices.GetSerializa
bleMembers(Type type, StreamingContext context)
at
System.Runtime.Serialization.Formatters.Binary.WriteObjectI
nfo.InitMemberInfo()
at
System.Runtime.Serialization.Formatters.Binary.WriteObjectI
nfo.InitSerialize(Object obj, ISurrogateSelector
surrogateSelector, StreamingContext context,
SerObjectInfoInit serObjectInfoInit, IFormatterConverter
converter)
at
System.Runtime.Serialization.Formatters.Binary.WriteObjectI
nfo.Serialize(Object obj, ISurrogateSelector
surrogateSelector, StreamingContext context,
SerObjectInfoInit serObjectInfoInit, IFormatterConverter
converter)
at
System.Runtime.Serialization.Formatters.Binary.ObjectWriter
.Serialize(Object graph, Header[] inHeaders,
__BinaryWriter serWriter, Boolean fCheck)
at
System.Runtime.Serialization.Formatters.Binary.BinaryFormat
ter.Serialize(Stream serializationStream, Object graph,
Header[] headers, Boolean fCheck)
at
System.Runtime.Serialization.Formatters.Binary.BinaryFormat
ter.Serialize(Stream serializationStream, Object graph,
Header[] headers)
at
System.Runtime.Remoting.Channels.CoreChannel.SerializeBinar
yMessage(IMessage msg, Stream outputStream, Boolean
includeVersions)
at
System.Runtime.Remoting.Channels.BinaryClientFormatterSink.
SerializeMessage(IMessage msg, ITransportHeaders& headers,
Stream& stream)
at
System.Runtime.Remoting.Channels.BinaryClientFormatterSink.
SyncProcessMessage(IMessage msg)


The object is complete in terms of being one object that
holds collections of other objects. Is that the problem?
I am lost. Any help would be appreciated.

Dan
 
B

Bob Powell [MVP]

The exception tells you the problem You haven't marked the object you're
trying to serialize as serializable.

Check out the SerializableAttribute

--
Bob Powell [MVP]
C#, System.Drawing

The October edition of Well Formed is now available.
Find out how to use DirectX in a Windows Forms control
http://www.bobpowell.net/currentissue.htm

Answer those GDI+ questions with the GDI+ FAQ
http://www.bobpowell.net/gdiplus_faq.htm

Read my Blog at http://bobpowelldotnet.blogspot.com

Dan said:
All I Am Attempting To Serialize An Object To An XML File.
Here Is The Code For That

public string SaveNewSurvey( MutualSurveyObject
mso_TempObject, int i_JobID )
{
string s_RootFileName;
string s_FinalFileName;

try
{

//Create The Final File Name
s_RootFileName = "job" + i_JobID.ToString() + "config.xml";
s_FinalFileName =
s_SetUpFilePath + @"\" + s_RootFileName;

//Serialize The Object And Write The XML Data
TextWriter tr = new StreamWriter( s_FinalFileName );
XmlSerializer sr = new XmlSerializer( typeof(
MutualSurveyObject ) );
sr.Serialize( tr, mso_TempObject );
tr.Close();

return s_RootFileName;

}

catch ( Exception e )
{
throw e;
}
}

However, When I attempt to execute this I get the
following rather long Exception message

System.Runtime.Serialization.SerializationException: The
type FNIS.MutualSurveyApplication.MutualSurveyObject in
Assembly MutualSurveyStructureObject,
Version=1.0.1411.14253, Culture=neutral,
PublicKeyToken=null is not marked as serializable.

Server stack trace:
at
System.Runtime.Serialization.FormatterServices.InternalGetS
erializableMembers(RuntimeType type, Boolean
excludeNonSerializable)
at
System.Runtime.Serialization.FormatterServices.GetSerializa
bleMembers(Type type, StreamingContext context)
at
System.Runtime.Serialization.Formatters.Binary.WriteObjectI
nfo.InitMemberInfo()
at
System.Runtime.Serialization.Formatters.Binary.WriteObjectI
nfo.InitSerialize(Object obj, ISurrogateSelector
surrogateSelector, StreamingContext context,
SerObjectInfoInit serObjectInfoInit, IFormatterConverter
converter)
at
System.Runtime.Serialization.Formatters.Binary.WriteObjectI
nfo.Serialize(Object obj, ISurrogateSelector
surrogateSelector, StreamingContext context,
SerObjectInfoInit serObjectInfoInit, IFormatterConverter
converter)
at
System.Runtime.Serialization.Formatters.Binary.ObjectWriter
.Serialize(Object graph, Header[] inHeaders,
__BinaryWriter serWriter, Boolean fCheck)
at
System.Runtime.Serialization.Formatters.Binary.BinaryFormat
ter.Serialize(Stream serializationStream, Object graph,
Header[] headers, Boolean fCheck)
at
System.Runtime.Serialization.Formatters.Binary.BinaryFormat
ter.Serialize(Stream serializationStream, Object graph,
Header[] headers)
at
System.Runtime.Remoting.Channels.CoreChannel.SerializeBinar
yMessage(IMessage msg, Stream outputStream, Boolean
includeVersions)
at
System.Runtime.Remoting.Channels.BinaryClientFormatterSink.
SerializeMessage(IMessage msg, ITransportHeaders& headers,
Stream& stream)
at
System.Runtime.Remoting.Channels.BinaryClientFormatterSink.
SyncProcessMessage(IMessage msg)


The object is complete in terms of being one object that
holds collections of other objects. Is that the problem?
I am lost. Any help would be appreciated.

Dan
 
M

Matt Burland

I assume your class is marked as Serializable, but you also need to check
that every object it contains is also either marked as Serializable itself
or marked as NonSerialized in your class.


Dan said:
All I Am Attempting To Serialize An Object To An XML File.
Here Is The Code For That

public string SaveNewSurvey( MutualSurveyObject
mso_TempObject, int i_JobID )
{
string s_RootFileName;
string s_FinalFileName;

try
{

//Create The Final File Name
s_RootFileName = "job" + i_JobID.ToString() + "config.xml";
s_FinalFileName =
s_SetUpFilePath + @"\" + s_RootFileName;

//Serialize The Object And Write The XML Data
TextWriter tr = new StreamWriter( s_FinalFileName );
XmlSerializer sr = new XmlSerializer( typeof(
MutualSurveyObject ) );
sr.Serialize( tr, mso_TempObject );
tr.Close();

return s_RootFileName;

}

catch ( Exception e )
{
throw e;
}
}

However, When I attempt to execute this I get the
following rather long Exception message

System.Runtime.Serialization.SerializationException: The
type FNIS.MutualSurveyApplication.MutualSurveyObject in
Assembly MutualSurveyStructureObject,
Version=1.0.1411.14253, Culture=neutral,
PublicKeyToken=null is not marked as serializable.

Server stack trace:
at
System.Runtime.Serialization.FormatterServices.InternalGetS
erializableMembers(RuntimeType type, Boolean
excludeNonSerializable)
at
System.Runtime.Serialization.FormatterServices.GetSerializa
bleMembers(Type type, StreamingContext context)
at
System.Runtime.Serialization.Formatters.Binary.WriteObjectI
nfo.InitMemberInfo()
at
System.Runtime.Serialization.Formatters.Binary.WriteObjectI
nfo.InitSerialize(Object obj, ISurrogateSelector
surrogateSelector, StreamingContext context,
SerObjectInfoInit serObjectInfoInit, IFormatterConverter
converter)
at
System.Runtime.Serialization.Formatters.Binary.WriteObjectI
nfo.Serialize(Object obj, ISurrogateSelector
surrogateSelector, StreamingContext context,
SerObjectInfoInit serObjectInfoInit, IFormatterConverter
converter)
at
System.Runtime.Serialization.Formatters.Binary.ObjectWriter
.Serialize(Object graph, Header[] inHeaders,
__BinaryWriter serWriter, Boolean fCheck)
at
System.Runtime.Serialization.Formatters.Binary.BinaryFormat
ter.Serialize(Stream serializationStream, Object graph,
Header[] headers, Boolean fCheck)
at
System.Runtime.Serialization.Formatters.Binary.BinaryFormat
ter.Serialize(Stream serializationStream, Object graph,
Header[] headers)
at
System.Runtime.Remoting.Channels.CoreChannel.SerializeBinar
yMessage(IMessage msg, Stream outputStream, Boolean
includeVersions)
at
System.Runtime.Remoting.Channels.BinaryClientFormatterSink.
SerializeMessage(IMessage msg, ITransportHeaders& headers,
Stream& stream)
at
System.Runtime.Remoting.Channels.BinaryClientFormatterSink.
SyncProcessMessage(IMessage msg)


The object is complete in terms of being one object that
holds collections of other objects. Is that the problem?
I am lost. Any help would be appreciated.

Dan
 
D

Dan

Bob And Nicholas,

Thanks for the quick replies. They are appreciated.

I want to make sure I am clear. In the class I am
attempting to Serialize I need to mark each element as
Serializable: is that correct? Some of the examples I
looked at did not mark the elements which gave me the
impression that you only marked those elements that you
wanted more control over.

Dan

-----Original Message-----
The exception tells you the problem You haven't marked the object you're
trying to serialize as serializable.

Check out the SerializableAttribute

--
Bob Powell [MVP]
C#, System.Drawing

The October edition of Well Formed is now available.
Find out how to use DirectX in a Windows Forms control
http://www.bobpowell.net/currentissue.htm

Answer those GDI+ questions with the GDI+ FAQ
http://www.bobpowell.net/gdiplus_faq.htm

Read my Blog at http://bobpowelldotnet.blogspot.com

Dan said:
All I Am Attempting To Serialize An Object To An XML File.
Here Is The Code For That

public string SaveNewSurvey( MutualSurveyObject
mso_TempObject, int i_JobID )
{
string s_RootFileName;
string s_FinalFileName;

try
{

//Create The Final File Name
s_RootFileName = "job" + i_JobID.ToString() + "config.xml";
s_FinalFileName =
s_SetUpFilePath + @"\" + s_RootFileName;

//Serialize The Object And Write The XML Data
TextWriter tr = new StreamWriter( s_FinalFileName );
XmlSerializer sr = new XmlSerializer( typeof(
MutualSurveyObject ) );
sr.Serialize( tr, mso_TempObject );
tr.Close();

return s_RootFileName;

}

catch ( Exception e )
{
throw e;
}
}

However, When I attempt to execute this I get the
following rather long Exception message

System.Runtime.Serialization.SerializationException: The
type FNIS.MutualSurveyApplication.MutualSurveyObject in
Assembly MutualSurveyStructureObject,
Version=1.0.1411.14253, Culture=neutral,
PublicKeyToken=null is not marked as serializable.

Server stack trace:
at
System.Runtime.Serialization.FormatterServices.InternalGetS
erializableMembers(RuntimeType type, Boolean
excludeNonSerializable)
at
System.Runtime.Serialization.FormatterServices.GetSerializa
bleMembers(Type type, StreamingContext context)
at
System.Runtime.Serialization.Formatters.Binary.WriteObjectI
nfo.InitMemberInfo()
at
System.Runtime.Serialization.Formatters.Binary.WriteObjectI
nfo.InitSerialize(Object obj, ISurrogateSelector
surrogateSelector, StreamingContext context,
SerObjectInfoInit serObjectInfoInit, IFormatterConverter
converter)
at
System.Runtime.Serialization.Formatters.Binary.WriteObjectI
nfo.Serialize(Object obj, ISurrogateSelector
surrogateSelector, StreamingContext context,
SerObjectInfoInit serObjectInfoInit, IFormatterConverter
converter)
at
System.Runtime.Serialization.Formatters.Binary.ObjectWriter
.Serialize(Object graph, Header[] inHeaders,
__BinaryWriter serWriter, Boolean fCheck)
at
System.Runtime.Serialization.Formatters.Binary.BinaryFormat
ter.Serialize(Stream serializationStream, Object graph,
Header[] headers, Boolean fCheck)
at
System.Runtime.Serialization.Formatters.Binary.BinaryFormat
ter.Serialize(Stream serializationStream, Object graph,
Header[] headers)
at
System.Runtime.Remoting.Channels.CoreChannel.SerializeBinar
yMessage(IMessage msg, Stream outputStream, Boolean
includeVersions)
at
System.Runtime.Remoting.Channels.BinaryClientFormatterSink.
SerializeMessage(IMessage msg, ITransportHeaders& headers,
Stream& stream)
at
System.Runtime.Remoting.Channels.BinaryClientFormatterSink.
SyncProcessMessage(IMessage msg)


The object is complete in terms of being one object that
holds collections of other objects. Is that the problem?
I am lost. Any help would be appreciated.

Dan


.
 
N

Nicholas Paldino [.NET/C# MVP]

Dan,

You should not have to mark the elements as Serializable. This is
required for formal serialization (using an IFormatter implementation), not
XML serialization.

--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Dan said:
Bob And Nicholas,

Thanks for the quick replies. They are appreciated.

I want to make sure I am clear. In the class I am
attempting to Serialize I need to mark each element as
Serializable: is that correct? Some of the examples I
looked at did not mark the elements which gave me the
impression that you only marked those elements that you
wanted more control over.

Dan

-----Original Message-----
The exception tells you the problem You haven't marked the object you're
trying to serialize as serializable.

Check out the SerializableAttribute

--
Bob Powell [MVP]
C#, System.Drawing

The October edition of Well Formed is now available.
Find out how to use DirectX in a Windows Forms control
http://www.bobpowell.net/currentissue.htm

Answer those GDI+ questions with the GDI+ FAQ
http://www.bobpowell.net/gdiplus_faq.htm

Read my Blog at http://bobpowelldotnet.blogspot.com

Dan said:
All I Am Attempting To Serialize An Object To An XML File.
Here Is The Code For That

public string SaveNewSurvey( MutualSurveyObject
mso_TempObject, int i_JobID )
{
string s_RootFileName;
string s_FinalFileName;

try
{

//Create The Final File Name
s_RootFileName = "job" + i_JobID.ToString() + "config.xml";
s_FinalFileName =
s_SetUpFilePath + @"\" + s_RootFileName;

//Serialize The Object And Write The XML Data
TextWriter tr = new StreamWriter( s_FinalFileName );
XmlSerializer sr = new XmlSerializer( typeof(
MutualSurveyObject ) );
sr.Serialize( tr, mso_TempObject );
tr.Close();

return s_RootFileName;

}

catch ( Exception e )
{
throw e;
}
}

However, When I attempt to execute this I get the
following rather long Exception message

System.Runtime.Serialization.SerializationException: The
type FNIS.MutualSurveyApplication.MutualSurveyObject in
Assembly MutualSurveyStructureObject,
Version=1.0.1411.14253, Culture=neutral,
PublicKeyToken=null is not marked as serializable.

Server stack trace:
at
System.Runtime.Serialization.FormatterServices.InternalGetS
erializableMembers(RuntimeType type, Boolean
excludeNonSerializable)
at
System.Runtime.Serialization.FormatterServices.GetSerializa
bleMembers(Type type, StreamingContext context)
at
System.Runtime.Serialization.Formatters.Binary.WriteObjectI
nfo.InitMemberInfo()
at
System.Runtime.Serialization.Formatters.Binary.WriteObjectI
nfo.InitSerialize(Object obj, ISurrogateSelector
surrogateSelector, StreamingContext context,
SerObjectInfoInit serObjectInfoInit, IFormatterConverter
converter)
at
System.Runtime.Serialization.Formatters.Binary.WriteObjectI
nfo.Serialize(Object obj, ISurrogateSelector
surrogateSelector, StreamingContext context,
SerObjectInfoInit serObjectInfoInit, IFormatterConverter
converter)
at
System.Runtime.Serialization.Formatters.Binary.ObjectWriter
.Serialize(Object graph, Header[] inHeaders,
__BinaryWriter serWriter, Boolean fCheck)
at
System.Runtime.Serialization.Formatters.Binary.BinaryFormat
ter.Serialize(Stream serializationStream, Object graph,
Header[] headers, Boolean fCheck)
at
System.Runtime.Serialization.Formatters.Binary.BinaryFormat
ter.Serialize(Stream serializationStream, Object graph,
Header[] headers)
at
System.Runtime.Remoting.Channels.CoreChannel.SerializeBinar
yMessage(IMessage msg, Stream outputStream, Boolean
includeVersions)
at
System.Runtime.Remoting.Channels.BinaryClientFormatterSink.
SerializeMessage(IMessage msg, ITransportHeaders& headers,
Stream& stream)
at
System.Runtime.Remoting.Channels.BinaryClientFormatterSink.
SyncProcessMessage(IMessage msg)


The object is complete in terms of being one object that
holds collections of other objects. Is that the problem?
I am lost. Any help would be appreciated.

Dan


.
 
G

Guest

ms-help://MS.VSCC.2003/MS.MSDNQTR.2003FEB.1033/cpguide/html/cpconintroducing
xmlserialization.htm


and

http://msdn.microsoft.com/library/d...ide/html/cpconintroducingxmlserialization.asp



Dan said:
Bob And Nicholas,

Thanks for the quick replies. They are appreciated.

I want to make sure I am clear. In the class I am
attempting to Serialize I need to mark each element as
Serializable: is that correct? Some of the examples I
looked at did not mark the elements which gave me the
impression that you only marked those elements that you
wanted more control over.

Dan

-----Original Message-----
The exception tells you the problem You haven't marked the object you're
trying to serialize as serializable.

Check out the SerializableAttribute

--
Bob Powell [MVP]
C#, System.Drawing

The October edition of Well Formed is now available.
Find out how to use DirectX in a Windows Forms control
http://www.bobpowell.net/currentissue.htm

Answer those GDI+ questions with the GDI+ FAQ
http://www.bobpowell.net/gdiplus_faq.htm

Read my Blog at http://bobpowelldotnet.blogspot.com

Dan said:
All I Am Attempting To Serialize An Object To An XML File.
Here Is The Code For That

public string SaveNewSurvey( MutualSurveyObject
mso_TempObject, int i_JobID )
{
string s_RootFileName;
string s_FinalFileName;

try
{

//Create The Final File Name
s_RootFileName = "job" + i_JobID.ToString() + "config.xml";
s_FinalFileName =
s_SetUpFilePath + @"\" + s_RootFileName;

//Serialize The Object And Write The XML Data
TextWriter tr = new StreamWriter( s_FinalFileName );
XmlSerializer sr = new XmlSerializer( typeof(
MutualSurveyObject ) );
sr.Serialize( tr, mso_TempObject );
tr.Close();

return s_RootFileName;

}

catch ( Exception e )
{
throw e;
}
}

However, When I attempt to execute this I get the
following rather long Exception message

System.Runtime.Serialization.SerializationException: The
type FNIS.MutualSurveyApplication.MutualSurveyObject in
Assembly MutualSurveyStructureObject,
Version=1.0.1411.14253, Culture=neutral,
PublicKeyToken=null is not marked as serializable.

Server stack trace:
at
System.Runtime.Serialization.FormatterServices.InternalGetS
erializableMembers(RuntimeType type, Boolean
excludeNonSerializable)
at
System.Runtime.Serialization.FormatterServices.GetSerializa
bleMembers(Type type, StreamingContext context)
at
System.Runtime.Serialization.Formatters.Binary.WriteObjectI
nfo.InitMemberInfo()
at
System.Runtime.Serialization.Formatters.Binary.WriteObjectI
nfo.InitSerialize(Object obj, ISurrogateSelector
surrogateSelector, StreamingContext context,
SerObjectInfoInit serObjectInfoInit, IFormatterConverter
converter)
at
System.Runtime.Serialization.Formatters.Binary.WriteObjectI
nfo.Serialize(Object obj, ISurrogateSelector
surrogateSelector, StreamingContext context,
SerObjectInfoInit serObjectInfoInit, IFormatterConverter
converter)
at
System.Runtime.Serialization.Formatters.Binary.ObjectWriter
.Serialize(Object graph, Header[] inHeaders,
__BinaryWriter serWriter, Boolean fCheck)
at
System.Runtime.Serialization.Formatters.Binary.BinaryFormat
ter.Serialize(Stream serializationStream, Object graph,
Header[] headers, Boolean fCheck)
at
System.Runtime.Serialization.Formatters.Binary.BinaryFormat
ter.Serialize(Stream serializationStream, Object graph,
Header[] headers)
at
System.Runtime.Remoting.Channels.CoreChannel.SerializeBinar
yMessage(IMessage msg, Stream outputStream, Boolean
includeVersions)
at
System.Runtime.Remoting.Channels.BinaryClientFormatterSink.
SerializeMessage(IMessage msg, ITransportHeaders& headers,
Stream& stream)
at
System.Runtime.Remoting.Channels.BinaryClientFormatterSink.
SyncProcessMessage(IMessage msg)


The object is complete in terms of being one object that
holds collections of other objects. Is that the problem?
I am lost. Any help would be appreciated.

Dan


.
 
D

Dan

Matt,

I am a bit confused. Looking at a simple example on MSDN I
don't see what you are refering to. Here is their class

public class OrderForm{
public DateTime OrderDate;
}

What kind of marking is needed? Thanks for the help. I
appreciate it.

Dan
-----Original Message-----
I assume your class is marked as Serializable, but you also need to check
that every object it contains is also either marked as Serializable itself
or marked as NonSerialized in your class.


Dan said:
All I Am Attempting To Serialize An Object To An XML File.
Here Is The Code For That

public string SaveNewSurvey( MutualSurveyObject
mso_TempObject, int i_JobID )
{
string s_RootFileName;
string s_FinalFileName;

try
{

//Create The Final File Name
s_RootFileName = "job" + i_JobID.ToString() + "config.xml";
s_FinalFileName =
s_SetUpFilePath + @"\" + s_RootFileName;

//Serialize The Object And Write The XML Data
TextWriter tr = new StreamWriter( s_FinalFileName );
XmlSerializer sr = new XmlSerializer( typeof(
MutualSurveyObject ) );
sr.Serialize( tr, mso_TempObject );
tr.Close();

return s_RootFileName;

}

catch ( Exception e )
{
throw e;
}
}

However, When I attempt to execute this I get the
following rather long Exception message

System.Runtime.Serialization.SerializationException: The
type FNIS.MutualSurveyApplication.MutualSurveyObject in
Assembly MutualSurveyStructureObject,
Version=1.0.1411.14253, Culture=neutral,
PublicKeyToken=null is not marked as serializable.

Server stack trace:
at
System.Runtime.Serialization.FormatterServices.InternalGetS
erializableMembers(RuntimeType type, Boolean
excludeNonSerializable)
at
System.Runtime.Serialization.FormatterServices.GetSerializa
bleMembers(Type type, StreamingContext context)
at
System.Runtime.Serialization.Formatters.Binary.WriteObjectI
nfo.InitMemberInfo()
at
System.Runtime.Serialization.Formatters.Binary.WriteObjectI
nfo.InitSerialize(Object obj, ISurrogateSelector
surrogateSelector, StreamingContext context,
SerObjectInfoInit serObjectInfoInit, IFormatterConverter
converter)
at
System.Runtime.Serialization.Formatters.Binary.WriteObjectI
nfo.Serialize(Object obj, ISurrogateSelector
surrogateSelector, StreamingContext context,
SerObjectInfoInit serObjectInfoInit, IFormatterConverter
converter)
at
System.Runtime.Serialization.Formatters.Binary.ObjectWriter
.Serialize(Object graph, Header[] inHeaders,
__BinaryWriter serWriter, Boolean fCheck)
at
System.Runtime.Serialization.Formatters.Binary.BinaryFormat
ter.Serialize(Stream serializationStream, Object graph,
Header[] headers, Boolean fCheck)
at
System.Runtime.Serialization.Formatters.Binary.BinaryFormat
ter.Serialize(Stream serializationStream, Object graph,
Header[] headers)
at
System.Runtime.Remoting.Channels.CoreChannel.SerializeBinar
yMessage(IMessage msg, Stream outputStream, Boolean
includeVersions)
at
System.Runtime.Remoting.Channels.BinaryClientFormatterSink.
SerializeMessage(IMessage msg, ITransportHeaders& headers,
Stream& stream)
at
System.Runtime.Remoting.Channels.BinaryClientFormatterSink.
SyncProcessMessage(IMessage msg)


The object is complete in terms of being one object that
holds collections of other objects. Is that the problem?
I am lost. Any help would be appreciated.

Dan


.
 
M

Matt Burland

Here's the deal (as I understand it), if you have a class like this:

[Serializable]
public class A
{
public B myB;
....
}

public class B
{
.....
}

And you want to serialize class A, you must:
- Mark class A with the Serializable attribute (which I've done here)
- Mark class B as Serializable as well because class A contains an
instance of class B (which I haven't done)
- Alternatively, I could mark the field myB as NonSerializable in the
definition of class A like this:

[NonSerialized()]
public B myB;

This will mean, however, that myB will not get saved when you serialize and
thus won't get restored when you reload it. Sometimes this might be exactly
what you want (for example if you class contains a reference to a form, you
probably don't want to save the entire form as well as the object itself).
Every field in your class that your are serializing must either:
- Be one of the base types (int, bool, etc) - in which case serialization is
already handled
- Be one of the framework types which is already marked as serializable (I
don't think they all are, check MSDN)
- Be of a type that is itself marked as Serializable in it's own definition
- Be marked as NonSerializable in your class definition - which means it
won't be saved

Hope that helps

Dan said:
Matt,

I am a bit confused. Looking at a simple example on MSDN I
don't see what you are refering to. Here is their class

public class OrderForm{
public DateTime OrderDate;
}

What kind of marking is needed? Thanks for the help. I
appreciate it.

Dan
-----Original Message-----
I assume your class is marked as Serializable, but you also need to check
that every object it contains is also either marked as Serializable itself
or marked as NonSerialized in your class.


Dan said:
All I Am Attempting To Serialize An Object To An XML File.
Here Is The Code For That

public string SaveNewSurvey( MutualSurveyObject
mso_TempObject, int i_JobID )
{
string s_RootFileName;
string s_FinalFileName;

try
{

//Create The Final File Name
s_RootFileName = "job" + i_JobID.ToString() + "config.xml";
s_FinalFileName =
s_SetUpFilePath + @"\" + s_RootFileName;

//Serialize The Object And Write The XML Data
TextWriter tr = new StreamWriter( s_FinalFileName );
XmlSerializer sr = new XmlSerializer( typeof(
MutualSurveyObject ) );
sr.Serialize( tr, mso_TempObject );
tr.Close();

return s_RootFileName;

}

catch ( Exception e )
{
throw e;
}
}

However, When I attempt to execute this I get the
following rather long Exception message

System.Runtime.Serialization.SerializationException: The
type FNIS.MutualSurveyApplication.MutualSurveyObject in
Assembly MutualSurveyStructureObject,
Version=1.0.1411.14253, Culture=neutral,
PublicKeyToken=null is not marked as serializable.

Server stack trace:
at
System.Runtime.Serialization.FormatterServices.InternalGetS
erializableMembers(RuntimeType type, Boolean
excludeNonSerializable)
at
System.Runtime.Serialization.FormatterServices.GetSerializa
bleMembers(Type type, StreamingContext context)
at
System.Runtime.Serialization.Formatters.Binary.WriteObjectI
nfo.InitMemberInfo()
at
System.Runtime.Serialization.Formatters.Binary.WriteObjectI
nfo.InitSerialize(Object obj, ISurrogateSelector
surrogateSelector, StreamingContext context,
SerObjectInfoInit serObjectInfoInit, IFormatterConverter
converter)
at
System.Runtime.Serialization.Formatters.Binary.WriteObjectI
nfo.Serialize(Object obj, ISurrogateSelector
surrogateSelector, StreamingContext context,
SerObjectInfoInit serObjectInfoInit, IFormatterConverter
converter)
at
System.Runtime.Serialization.Formatters.Binary.ObjectWriter
.Serialize(Object graph, Header[] inHeaders,
__BinaryWriter serWriter, Boolean fCheck)
at
System.Runtime.Serialization.Formatters.Binary.BinaryFormat
ter.Serialize(Stream serializationStream, Object graph,
Header[] headers, Boolean fCheck)
at
System.Runtime.Serialization.Formatters.Binary.BinaryFormat
ter.Serialize(Stream serializationStream, Object graph,
Header[] headers)
at
System.Runtime.Remoting.Channels.CoreChannel.SerializeBinar
yMessage(IMessage msg, Stream outputStream, Boolean
includeVersions)
at
System.Runtime.Remoting.Channels.BinaryClientFormatterSink.
SerializeMessage(IMessage msg, ITransportHeaders& headers,
Stream& stream)
at
System.Runtime.Remoting.Channels.BinaryClientFormatterSink.
SyncProcessMessage(IMessage msg)


The object is complete in terms of being one object that
holds collections of other objects. Is that the problem?
I am lost. Any help would be appreciated.

Dan


.
 
B

Bob Powell [MVP]

To clarify..

Mark the class as serializable..

[Serializable()]
class MyClass
{
}

All public properties will be serialized. If you have objects in those
properties they will also need to be marked as serializable.

If you don't want a particlar property to be serialized as XML mark it as
XmlIgnore()...

[XmlIgnore()]
public int MyNonSerializedInt
{
get...
set...
}

If you want to change the name of a property to something else use the
XmlElement attribute...

[XmlElement("X")]
public int MyRidiculouslyUnweildyAndGratuituoslyLongNamedIntProperty
{
get...
set...
}

HTH

--
Bob Powell [MVP]
C#, System.Drawing

The October edition of Well Formed is now available.
Find out how to use DirectX in a Windows Forms control
http://www.bobpowell.net/currentissue.htm

Answer those GDI+ questions with the GDI+ FAQ
http://www.bobpowell.net/gdiplus_faq.htm

Read my Blog at http://bobpowelldotnet.blogspot.com

Dan said:
Bob And Nicholas,

Thanks for the quick replies. They are appreciated.

I want to make sure I am clear. In the class I am
attempting to Serialize I need to mark each element as
Serializable: is that correct? Some of the examples I
looked at did not mark the elements which gave me the
impression that you only marked those elements that you
wanted more control over.

Dan

-----Original Message-----
The exception tells you the problem You haven't marked the object you're
trying to serialize as serializable.

Check out the SerializableAttribute

--
Bob Powell [MVP]
C#, System.Drawing

The October edition of Well Formed is now available.
Find out how to use DirectX in a Windows Forms control
http://www.bobpowell.net/currentissue.htm

Answer those GDI+ questions with the GDI+ FAQ
http://www.bobpowell.net/gdiplus_faq.htm

Read my Blog at http://bobpowelldotnet.blogspot.com

Dan said:
All I Am Attempting To Serialize An Object To An XML File.
Here Is The Code For That

public string SaveNewSurvey( MutualSurveyObject
mso_TempObject, int i_JobID )
{
string s_RootFileName;
string s_FinalFileName;

try
{

//Create The Final File Name
s_RootFileName = "job" + i_JobID.ToString() + "config.xml";
s_FinalFileName =
s_SetUpFilePath + @"\" + s_RootFileName;

//Serialize The Object And Write The XML Data
TextWriter tr = new StreamWriter( s_FinalFileName );
XmlSerializer sr = new XmlSerializer( typeof(
MutualSurveyObject ) );
sr.Serialize( tr, mso_TempObject );
tr.Close();

return s_RootFileName;

}

catch ( Exception e )
{
throw e;
}
}

However, When I attempt to execute this I get the
following rather long Exception message

System.Runtime.Serialization.SerializationException: The
type FNIS.MutualSurveyApplication.MutualSurveyObject in
Assembly MutualSurveyStructureObject,
Version=1.0.1411.14253, Culture=neutral,
PublicKeyToken=null is not marked as serializable.

Server stack trace:
at
System.Runtime.Serialization.FormatterServices.InternalGetS
erializableMembers(RuntimeType type, Boolean
excludeNonSerializable)
at
System.Runtime.Serialization.FormatterServices.GetSerializa
bleMembers(Type type, StreamingContext context)
at
System.Runtime.Serialization.Formatters.Binary.WriteObjectI
nfo.InitMemberInfo()
at
System.Runtime.Serialization.Formatters.Binary.WriteObjectI
nfo.InitSerialize(Object obj, ISurrogateSelector
surrogateSelector, StreamingContext context,
SerObjectInfoInit serObjectInfoInit, IFormatterConverter
converter)
at
System.Runtime.Serialization.Formatters.Binary.WriteObjectI
nfo.Serialize(Object obj, ISurrogateSelector
surrogateSelector, StreamingContext context,
SerObjectInfoInit serObjectInfoInit, IFormatterConverter
converter)
at
System.Runtime.Serialization.Formatters.Binary.ObjectWriter
.Serialize(Object graph, Header[] inHeaders,
__BinaryWriter serWriter, Boolean fCheck)
at
System.Runtime.Serialization.Formatters.Binary.BinaryFormat
ter.Serialize(Stream serializationStream, Object graph,
Header[] headers, Boolean fCheck)
at
System.Runtime.Serialization.Formatters.Binary.BinaryFormat
ter.Serialize(Stream serializationStream, Object graph,
Header[] headers)
at
System.Runtime.Remoting.Channels.CoreChannel.SerializeBinar
yMessage(IMessage msg, Stream outputStream, Boolean
includeVersions)
at
System.Runtime.Remoting.Channels.BinaryClientFormatterSink.
SerializeMessage(IMessage msg, ITransportHeaders& headers,
Stream& stream)
at
System.Runtime.Remoting.Channels.BinaryClientFormatterSink.
SyncProcessMessage(IMessage msg)


The object is complete in terms of being one object that
holds collections of other objects. Is that the problem?
I am lost. Any help would be appreciated.

Dan


.
 
D

Dan

To Bob, Nicholas, Matt, and the one unknown responder.

Thank you so much for taking the time to answer my
questions. Because you were all kind enough to take 5
minutes out of your day and respond, you have saved me
countless hours of frustration and head pounding. I am
happy to say my object is being searlized into a beautiful
xml file.

Thanks again!!!!!

Dan
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top