473,686 Members | 2,107 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Serialize Class To XML

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

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

try
{

//Create The Final File Name
s_RootFileName = "job" + i_JobID.ToStrin g() + "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(
MutualSurveyObj ect ) );
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.S erializationExc eption: The
type FNIS.MutualSurv eyApplication.M utualSurveyObje ct in
Assembly MutualSurveyStr uctureObject,
Version=1.0.141 1.14253, Culture=neutral ,
PublicKeyToken= null is not marked as serializable.

Server stack trace:
at
System.Runtime. Serialization.F ormatterService s.InternalGetS
erializableMemb ers(RuntimeType type, Boolean
excludeNonSeria lizable)
at
System.Runtime. Serialization.F ormatterService s.GetSerializa
bleMembers(Type type, StreamingContex t context)
at
System.Runtime. Serialization.F ormatters.Binar y.WriteObjectI
nfo.InitMemberI nfo()
at
System.Runtime. Serialization.F ormatters.Binar y.WriteObjectI
nfo.InitSeriali ze(Object obj, ISurrogateSelec tor
surrogateSelect or, StreamingContex t context,
SerObjectInfoIn it serObjectInfoIn it, IFormatterConve rter
converter)
at
System.Runtime. Serialization.F ormatters.Binar y.WriteObjectI
nfo.Serialize(O bject obj, ISurrogateSelec tor
surrogateSelect or, StreamingContex t context,
SerObjectInfoIn it serObjectInfoIn it, IFormatterConve rter
converter)
at
System.Runtime. Serialization.F ormatters.Binar y.ObjectWriter
..Serialize(Obj ect graph, Header[] inHeaders,
__BinaryWriter serWriter, Boolean fCheck)
at
System.Runtime. Serialization.F ormatters.Binar y.BinaryFormat
ter.Serialize(S tream serializationSt ream, Object graph,
Header[] headers, Boolean fCheck)
at
System.Runtime. Serialization.F ormatters.Binar y.BinaryFormat
ter.Serialize(S tream serializationSt ream, Object graph,
Header[] headers)
at
System.Runtime. Remoting.Channe ls.CoreChannel. SerializeBinar
yMessage(IMessa ge msg, Stream outputStream, Boolean
includeVersions )
at
System.Runtime. Remoting.Channe ls.BinaryClient FormatterSink.
SerializeMessag e(IMessage msg, ITransportHeade rs& headers,
Stream& stream)
at
System.Runtime. Remoting.Channe ls.BinaryClient FormatterSink.
SyncProcessMess age(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

Nov 15 '05 #1
10 4150
The exception tells you the problem You haven't marked the object you're
trying to serialize as serializable.

Check out the SerializableAtt ribute

--
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" <dd*****@fnisol utions.com> wrote in message
news:22******** *************** *****@phx.gbl.. .
All I Am Attempting To Serialize An Object To An XML File.
Here Is The Code For That

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

try
{

//Create The Final File Name
s_RootFileName = "job" + i_JobID.ToStrin g() + "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(
MutualSurveyObj ect ) );
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.S erializationExc eption: The
type FNIS.MutualSurv eyApplication.M utualSurveyObje ct in
Assembly MutualSurveyStr uctureObject,
Version=1.0.141 1.14253, Culture=neutral ,
PublicKeyToken= null is not marked as serializable.

Server stack trace:
at
System.Runtime. Serialization.F ormatterService s.InternalGetS
erializableMemb ers(RuntimeType type, Boolean
excludeNonSeria lizable)
at
System.Runtime. Serialization.F ormatterService s.GetSerializa
bleMembers(Type type, StreamingContex t context)
at
System.Runtime. Serialization.F ormatters.Binar y.WriteObjectI
nfo.InitMemberI nfo()
at
System.Runtime. Serialization.F ormatters.Binar y.WriteObjectI
nfo.InitSeriali ze(Object obj, ISurrogateSelec tor
surrogateSelect or, StreamingContex t context,
SerObjectInfoIn it serObjectInfoIn it, IFormatterConve rter
converter)
at
System.Runtime. Serialization.F ormatters.Binar y.WriteObjectI
nfo.Serialize(O bject obj, ISurrogateSelec tor
surrogateSelect or, StreamingContex t context,
SerObjectInfoIn it serObjectInfoIn it, IFormatterConve rter
converter)
at
System.Runtime. Serialization.F ormatters.Binar y.ObjectWriter
.Serialize(Obje ct graph, Header[] inHeaders,
__BinaryWriter serWriter, Boolean fCheck)
at
System.Runtime. Serialization.F ormatters.Binar y.BinaryFormat
ter.Serialize(S tream serializationSt ream, Object graph,
Header[] headers, Boolean fCheck)
at
System.Runtime. Serialization.F ormatters.Binar y.BinaryFormat
ter.Serialize(S tream serializationSt ream, Object graph,
Header[] headers)
at
System.Runtime. Remoting.Channe ls.CoreChannel. SerializeBinar
yMessage(IMessa ge msg, Stream outputStream, Boolean
includeVersions )
at
System.Runtime. Remoting.Channe ls.BinaryClient FormatterSink.
SerializeMessag e(IMessage msg, ITransportHeade rs& headers,
Stream& stream)
at
System.Runtime. Remoting.Channe ls.BinaryClient FormatterSink.
SyncProcessMess age(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

Nov 15 '05 #2
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]
- mv*@spam.guard. caspershouse.co m

"Dan" <dd*****@fnisol utions.com> wrote in message
news:22******** *************** *****@phx.gbl.. .
All I Am Attempting To Serialize An Object To An XML File.
Here Is The Code For That

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

try
{

//Create The Final File Name
s_RootFileName = "job" + i_JobID.ToStrin g() + "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(
MutualSurveyObj ect ) );
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.S erializationExc eption: The
type FNIS.MutualSurv eyApplication.M utualSurveyObje ct in
Assembly MutualSurveyStr uctureObject,
Version=1.0.141 1.14253, Culture=neutral ,
PublicKeyToken= null is not marked as serializable.

Server stack trace:
at
System.Runtime. Serialization.F ormatterService s.InternalGetS
erializableMemb ers(RuntimeType type, Boolean
excludeNonSeria lizable)
at
System.Runtime. Serialization.F ormatterService s.GetSerializa
bleMembers(Type type, StreamingContex t context)
at
System.Runtime. Serialization.F ormatters.Binar y.WriteObjectI
nfo.InitMemberI nfo()
at
System.Runtime. Serialization.F ormatters.Binar y.WriteObjectI
nfo.InitSeriali ze(Object obj, ISurrogateSelec tor
surrogateSelect or, StreamingContex t context,
SerObjectInfoIn it serObjectInfoIn it, IFormatterConve rter
converter)
at
System.Runtime. Serialization.F ormatters.Binar y.WriteObjectI
nfo.Serialize(O bject obj, ISurrogateSelec tor
surrogateSelect or, StreamingContex t context,
SerObjectInfoIn it serObjectInfoIn it, IFormatterConve rter
converter)
at
System.Runtime. Serialization.F ormatters.Binar y.ObjectWriter
.Serialize(Obje ct graph, Header[] inHeaders,
__BinaryWriter serWriter, Boolean fCheck)
at
System.Runtime. Serialization.F ormatters.Binar y.BinaryFormat
ter.Serialize(S tream serializationSt ream, Object graph,
Header[] headers, Boolean fCheck)
at
System.Runtime. Serialization.F ormatters.Binar y.BinaryFormat
ter.Serialize(S tream serializationSt ream, Object graph,
Header[] headers)
at
System.Runtime. Remoting.Channe ls.CoreChannel. SerializeBinar
yMessage(IMessa ge msg, Stream outputStream, Boolean
includeVersions )
at
System.Runtime. Remoting.Channe ls.BinaryClient FormatterSink.
SerializeMessag e(IMessage msg, ITransportHeade rs& headers,
Stream& stream)
at
System.Runtime. Remoting.Channe ls.BinaryClient FormatterSink.
SyncProcessMess age(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

Nov 15 '05 #3
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" <dd*****@fnisol utions.com> wrote in message
news:22******** *************** *****@phx.gbl.. .
All I Am Attempting To Serialize An Object To An XML File.
Here Is The Code For That

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

try
{

//Create The Final File Name
s_RootFileName = "job" + i_JobID.ToStrin g() + "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(
MutualSurveyObj ect ) );
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.S erializationExc eption: The
type FNIS.MutualSurv eyApplication.M utualSurveyObje ct in
Assembly MutualSurveyStr uctureObject,
Version=1.0.141 1.14253, Culture=neutral ,
PublicKeyToken= null is not marked as serializable.

Server stack trace:
at
System.Runtime. Serialization.F ormatterService s.InternalGetS
erializableMemb ers(RuntimeType type, Boolean
excludeNonSeria lizable)
at
System.Runtime. Serialization.F ormatterService s.GetSerializa
bleMembers(Type type, StreamingContex t context)
at
System.Runtime. Serialization.F ormatters.Binar y.WriteObjectI
nfo.InitMemberI nfo()
at
System.Runtime. Serialization.F ormatters.Binar y.WriteObjectI
nfo.InitSeriali ze(Object obj, ISurrogateSelec tor
surrogateSelect or, StreamingContex t context,
SerObjectInfoIn it serObjectInfoIn it, IFormatterConve rter
converter)
at
System.Runtime. Serialization.F ormatters.Binar y.WriteObjectI
nfo.Serialize(O bject obj, ISurrogateSelec tor
surrogateSelect or, StreamingContex t context,
SerObjectInfoIn it serObjectInfoIn it, IFormatterConve rter
converter)
at
System.Runtime. Serialization.F ormatters.Binar y.ObjectWriter
.Serialize(Obje ct graph, Header[] inHeaders,
__BinaryWriter serWriter, Boolean fCheck)
at
System.Runtime. Serialization.F ormatters.Binar y.BinaryFormat
ter.Serialize(S tream serializationSt ream, Object graph,
Header[] headers, Boolean fCheck)
at
System.Runtime. Serialization.F ormatters.Binar y.BinaryFormat
ter.Serialize(S tream serializationSt ream, Object graph,
Header[] headers)
at
System.Runtime. Remoting.Channe ls.CoreChannel. SerializeBinar
yMessage(IMessa ge msg, Stream outputStream, Boolean
includeVersions )
at
System.Runtime. Remoting.Channe ls.BinaryClient FormatterSink.
SerializeMessag e(IMessage msg, ITransportHeade rs& headers,
Stream& stream)
at
System.Runtime. Remoting.Channe ls.BinaryClient FormatterSink.
SyncProcessMess age(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

Nov 15 '05 #4
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'retrying to serialize as serializable.

Check out the SerializableAtt ribute

--
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" <dd*****@fnisol utions.com> wrote in message
news:22******* *************** ******@phx.gbl. ..
All I Am Attempting To Serialize An Object To An XML File. Here Is The Code For That

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

try
{

//Create The Final File Name
s_RootFileName = "job" + i_JobID.ToStrin g() + "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(
MutualSurveyObj ect ) );
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.S erializationExc eption: The
type FNIS.MutualSurv eyApplication.M utualSurveyObje ct in
Assembly MutualSurveyStr uctureObject,
Version=1.0.141 1.14253, Culture=neutral ,
PublicKeyToken= null is not marked as serializable.

Server stack trace:
at
System.Runtime. Serialization.F ormatterService s.InternalGetS erializableMemb ers(RuntimeType type, Boolean
excludeNonSeria lizable)
at
System.Runtime. Serialization.F ormatterService s.GetSerializa bleMembers(Type type, StreamingContex t context)
at
System.Runtime. Serialization.F ormatters.Binar y.WriteObjectI nfo.InitMemberI nfo()
at
System.Runtime. Serialization.F ormatters.Binar y.WriteObjectI nfo.InitSeriali ze(Object obj, ISurrogateSelec tor
surrogateSelect or, StreamingContex t context,
SerObjectInfoIn it serObjectInfoIn it, IFormatterConve rter
converter)
at
System.Runtime. Serialization.F ormatters.Binar y.WriteObjectI nfo.Serialize(O bject obj, ISurrogateSelec tor
surrogateSelect or, StreamingContex t context,
SerObjectInfoIn it serObjectInfoIn it, IFormatterConve rter
converter)
at
System.Runtime. Serialization.F ormatters.Binar y.ObjectWriter .Serialize(Obje ct graph, Header[] inHeaders,
__BinaryWriter serWriter, Boolean fCheck)
at
System.Runtime. Serialization.F ormatters.Binar y.BinaryFormat ter.Serialize(S tream serializationSt ream, Object graph,
Header[] headers, Boolean fCheck)
at
System.Runtime. Serialization.F ormatters.Binar y.BinaryFormat ter.Serialize(S tream serializationSt ream, Object graph,
Header[] headers)
at
System.Runtime. Remoting.Channe ls.CoreChannel. SerializeBinar yMessage(IMessa ge msg, Stream outputStream, Boolean
includeVersions )
at
System.Runtime. Remoting.Channe ls.BinaryClient FormatterSink. SerializeMessag e(IMessage msg, ITransportHeade rs& headers, Stream& stream)
at
System.Runtime. Remoting.Channe ls.BinaryClient FormatterSink. SyncProcessMess age(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

.

Nov 15 '05 #5
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]
- mv*@spam.guard. caspershouse.co m

"Dan" <an*******@disc ussions.microso ft.com> wrote in message
news:22******** *************** *****@phx.gbl.. .
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 SerializableAtt ribute

--
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" <dd*****@fnisol utions.com> wrote in message
news:22******* *************** ******@phx.gbl. ..
All I Am Attempting To Serialize An Object To An XML File. Here Is The Code For That

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

try
{

//Create The Final File Name
s_RootFileName = "job" + i_JobID.ToStrin g() + "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(
MutualSurveyObj ect ) );
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.S erializationExc eption: The
type FNIS.MutualSurv eyApplication.M utualSurveyObje ct in
Assembly MutualSurveyStr uctureObject,
Version=1.0.141 1.14253, Culture=neutral ,
PublicKeyToken= null is not marked as serializable.

Server stack trace:
at
System.Runtime. Serialization.F ormatterService s.InternalGetS erializableMemb ers(RuntimeType type, Boolean
excludeNonSeria lizable)
at
System.Runtime. Serialization.F ormatterService s.GetSerializa bleMembers(Type type, StreamingContex t context)
at
System.Runtime. Serialization.F ormatters.Binar y.WriteObjectI nfo.InitMemberI nfo()
at
System.Runtime. Serialization.F ormatters.Binar y.WriteObjectI nfo.InitSeriali ze(Object obj, ISurrogateSelec tor
surrogateSelect or, StreamingContex t context,
SerObjectInfoIn it serObjectInfoIn it, IFormatterConve rter
converter)
at
System.Runtime. Serialization.F ormatters.Binar y.WriteObjectI nfo.Serialize(O bject obj, ISurrogateSelec tor
surrogateSelect or, StreamingContex t context,
SerObjectInfoIn it serObjectInfoIn it, IFormatterConve rter
converter)
at
System.Runtime. Serialization.F ormatters.Binar y.ObjectWriter .Serialize(Obje ct graph, Header[] inHeaders,
__BinaryWriter serWriter, Boolean fCheck)
at
System.Runtime. Serialization.F ormatters.Binar y.BinaryFormat ter.Serialize(S tream serializationSt ream, Object graph,
Header[] headers, Boolean fCheck)
at
System.Runtime. Serialization.F ormatters.Binar y.BinaryFormat ter.Serialize(S tream serializationSt ream, Object graph,
Header[] headers)
at
System.Runtime. Remoting.Channe ls.CoreChannel. SerializeBinar yMessage(IMessa ge msg, Stream outputStream, Boolean
includeVersions )
at
System.Runtime. Remoting.Channe ls.BinaryClient FormatterSink. SerializeMessag e(IMessage msg, ITransportHeade rs& headers, Stream& stream)
at
System.Runtime. Remoting.Channe ls.BinaryClient FormatterSink. SyncProcessMess age(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

.

Nov 15 '05 #6
ms-help://MS.VSCC.2003/MS.MSDNQTR.2003 FEB.1033/cpguide/html/cpconintroducin g
xmlserializatio n.htm
and

http://msdn.microsoft.com/library/de...ialization.asp

"Dan" <an*******@disc ussions.microso ft.com> wrote in message
news:22******** *************** *****@phx.gbl.. .
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 SerializableAtt ribute

--
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" <dd*****@fnisol utions.com> wrote in message
news:22******* *************** ******@phx.gbl. ..
All I Am Attempting To Serialize An Object To An XML File. Here Is The Code For That

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

try
{

//Create The Final File Name
s_RootFileName = "job" + i_JobID.ToStrin g() + "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(
MutualSurveyObj ect ) );
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.S erializationExc eption: The
type FNIS.MutualSurv eyApplication.M utualSurveyObje ct in
Assembly MutualSurveyStr uctureObject,
Version=1.0.141 1.14253, Culture=neutral ,
PublicKeyToken= null is not marked as serializable.

Server stack trace:
at
System.Runtime. Serialization.F ormatterService s.InternalGetS erializableMemb ers(RuntimeType type, Boolean
excludeNonSeria lizable)
at
System.Runtime. Serialization.F ormatterService s.GetSerializa bleMembers(Type type, StreamingContex t context)
at
System.Runtime. Serialization.F ormatters.Binar y.WriteObjectI nfo.InitMemberI nfo()
at
System.Runtime. Serialization.F ormatters.Binar y.WriteObjectI nfo.InitSeriali ze(Object obj, ISurrogateSelec tor
surrogateSelect or, StreamingContex t context,
SerObjectInfoIn it serObjectInfoIn it, IFormatterConve rter
converter)
at
System.Runtime. Serialization.F ormatters.Binar y.WriteObjectI nfo.Serialize(O bject obj, ISurrogateSelec tor
surrogateSelect or, StreamingContex t context,
SerObjectInfoIn it serObjectInfoIn it, IFormatterConve rter
converter)
at
System.Runtime. Serialization.F ormatters.Binar y.ObjectWriter .Serialize(Obje ct graph, Header[] inHeaders,
__BinaryWriter serWriter, Boolean fCheck)
at
System.Runtime. Serialization.F ormatters.Binar y.BinaryFormat ter.Serialize(S tream serializationSt ream, Object graph,
Header[] headers, Boolean fCheck)
at
System.Runtime. Serialization.F ormatters.Binar y.BinaryFormat ter.Serialize(S tream serializationSt ream, Object graph,
Header[] headers)
at
System.Runtime. Remoting.Channe ls.CoreChannel. SerializeBinar yMessage(IMessa ge msg, Stream outputStream, Boolean
includeVersions )
at
System.Runtime. Remoting.Channe ls.BinaryClient FormatterSink. SerializeMessag e(IMessage msg, ITransportHeade rs& headers, Stream& stream)
at
System.Runtime. Remoting.Channe ls.BinaryClient FormatterSink. SyncProcessMess age(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

.

Nov 15 '05 #7
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 checkthat every object it contains is also either marked as Serializable itselfor marked as NonSerialized in your class.
"Dan" <dd*****@fnisol utions.com> wrote in message
news:22******* *************** ******@phx.gbl. ..
All I Am Attempting To Serialize An Object To An XML File. Here Is The Code For That

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

try
{

//Create The Final File Name
s_RootFileName = "job" + i_JobID.ToStrin g() + "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(
MutualSurveyObj ect ) );
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.S erializationExc eption: The
type FNIS.MutualSurv eyApplication.M utualSurveyObje ct in
Assembly MutualSurveyStr uctureObject,
Version=1.0.141 1.14253, Culture=neutral ,
PublicKeyToken= null is not marked as serializable.

Server stack trace:
at
System.Runtime. Serialization.F ormatterService s.InternalGetS erializableMemb ers(RuntimeType type, Boolean
excludeNonSeria lizable)
at
System.Runtime. Serialization.F ormatterService s.GetSerializa bleMembers(Type type, StreamingContex t context)
at
System.Runtime. Serialization.F ormatters.Binar y.WriteObjectI nfo.InitMemberI nfo()
at
System.Runtime. Serialization.F ormatters.Binar y.WriteObjectI nfo.InitSeriali ze(Object obj, ISurrogateSelec tor
surrogateSelect or, StreamingContex t context,
SerObjectInfoIn it serObjectInfoIn it, IFormatterConve rter
converter)
at
System.Runtime. Serialization.F ormatters.Binar y.WriteObjectI nfo.Serialize(O bject obj, ISurrogateSelec tor
surrogateSelect or, StreamingContex t context,
SerObjectInfoIn it serObjectInfoIn it, IFormatterConve rter
converter)
at
System.Runtime. Serialization.F ormatters.Binar y.ObjectWriter .Serialize(Obje ct graph, Header[] inHeaders,
__BinaryWriter serWriter, Boolean fCheck)
at
System.Runtime. Serialization.F ormatters.Binar y.BinaryFormat ter.Serialize(S tream serializationSt ream, Object graph,
Header[] headers, Boolean fCheck)
at
System.Runtime. Serialization.F ormatters.Binar y.BinaryFormat ter.Serialize(S tream serializationSt ream, Object graph,
Header[] headers)
at
System.Runtime. Remoting.Channe ls.CoreChannel. SerializeBinar yMessage(IMessa ge msg, Stream outputStream, Boolean
includeVersions )
at
System.Runtime. Remoting.Channe ls.BinaryClient FormatterSink. SerializeMessag e(IMessage msg, ITransportHeade rs& headers, Stream& stream)
at
System.Runtime. Remoting.Channe ls.BinaryClient FormatterSink. SyncProcessMess age(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

.

Nov 15 '05 #8
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" <an*******@disc ussions.microso ft.com> wrote in message
news:07******** *************** *****@phx.gbl.. .
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" <dd*****@fnisol utions.com> wrote in message
news:22******* *************** ******@phx.gbl. ..
All I Am Attempting To Serialize An Object To An XML File. Here Is The Code For That

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

try
{

//Create The Final File Name
s_RootFileName = "job" + i_JobID.ToStrin g() + "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(
MutualSurveyObj ect ) );
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.S erializationExc eption: The
type FNIS.MutualSurv eyApplication.M utualSurveyObje ct in
Assembly MutualSurveyStr uctureObject,
Version=1.0.141 1.14253, Culture=neutral ,
PublicKeyToken= null is not marked as serializable.

Server stack trace:
at
System.Runtime. Serialization.F ormatterService s.InternalGetS erializableMemb ers(RuntimeType type, Boolean
excludeNonSeria lizable)
at
System.Runtime. Serialization.F ormatterService s.GetSerializa bleMembers(Type type, StreamingContex t context)
at
System.Runtime. Serialization.F ormatters.Binar y.WriteObjectI nfo.InitMemberI nfo()
at
System.Runtime. Serialization.F ormatters.Binar y.WriteObjectI nfo.InitSeriali ze(Object obj, ISurrogateSelec tor
surrogateSelect or, StreamingContex t context,
SerObjectInfoIn it serObjectInfoIn it, IFormatterConve rter
converter)
at
System.Runtime. Serialization.F ormatters.Binar y.WriteObjectI nfo.Serialize(O bject obj, ISurrogateSelec tor
surrogateSelect or, StreamingContex t context,
SerObjectInfoIn it serObjectInfoIn it, IFormatterConve rter
converter)
at
System.Runtime. Serialization.F ormatters.Binar y.ObjectWriter .Serialize(Obje ct graph, Header[] inHeaders,
__BinaryWriter serWriter, Boolean fCheck)
at
System.Runtime. Serialization.F ormatters.Binar y.BinaryFormat ter.Serialize(S tream serializationSt ream, Object graph,
Header[] headers, Boolean fCheck)
at
System.Runtime. Serialization.F ormatters.Binar y.BinaryFormat ter.Serialize(S tream serializationSt ream, Object graph,
Header[] headers)
at
System.Runtime. Remoting.Channe ls.CoreChannel. SerializeBinar yMessage(IMessa ge msg, Stream outputStream, Boolean
includeVersions )
at
System.Runtime. Remoting.Channe ls.BinaryClient FormatterSink. SerializeMessag e(IMessage msg, ITransportHeade rs& headers, Stream& stream)
at
System.Runtime. Remoting.Channe ls.BinaryClient FormatterSink. SyncProcessMess age(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

.

Nov 15 '05 #9
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 MyNonSerialized Int
{
get...
set...
}

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

[XmlElement("X")]
public int MyRidiculouslyU nweildyAndGratu ituoslyLongName dIntProperty
{
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" <an*******@disc ussions.microso ft.com> wrote in message
news:22******** *************** *****@phx.gbl.. .
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 SerializableAtt ribute

--
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" <dd*****@fnisol utions.com> wrote in message
news:22******* *************** ******@phx.gbl. ..
All I Am Attempting To Serialize An Object To An XML File. Here Is The Code For That

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

try
{

//Create The Final File Name
s_RootFileName = "job" + i_JobID.ToStrin g() + "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(
MutualSurveyObj ect ) );
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.S erializationExc eption: The
type FNIS.MutualSurv eyApplication.M utualSurveyObje ct in
Assembly MutualSurveyStr uctureObject,
Version=1.0.141 1.14253, Culture=neutral ,
PublicKeyToken= null is not marked as serializable.

Server stack trace:
at
System.Runtime. Serialization.F ormatterService s.InternalGetS erializableMemb ers(RuntimeType type, Boolean
excludeNonSeria lizable)
at
System.Runtime. Serialization.F ormatterService s.GetSerializa bleMembers(Type type, StreamingContex t context)
at
System.Runtime. Serialization.F ormatters.Binar y.WriteObjectI nfo.InitMemberI nfo()
at
System.Runtime. Serialization.F ormatters.Binar y.WriteObjectI nfo.InitSeriali ze(Object obj, ISurrogateSelec tor
surrogateSelect or, StreamingContex t context,
SerObjectInfoIn it serObjectInfoIn it, IFormatterConve rter
converter)
at
System.Runtime. Serialization.F ormatters.Binar y.WriteObjectI nfo.Serialize(O bject obj, ISurrogateSelec tor
surrogateSelect or, StreamingContex t context,
SerObjectInfoIn it serObjectInfoIn it, IFormatterConve rter
converter)
at
System.Runtime. Serialization.F ormatters.Binar y.ObjectWriter .Serialize(Obje ct graph, Header[] inHeaders,
__BinaryWriter serWriter, Boolean fCheck)
at
System.Runtime. Serialization.F ormatters.Binar y.BinaryFormat ter.Serialize(S tream serializationSt ream, Object graph,
Header[] headers, Boolean fCheck)
at
System.Runtime. Serialization.F ormatters.Binar y.BinaryFormat ter.Serialize(S tream serializationSt ream, Object graph,
Header[] headers)
at
System.Runtime. Remoting.Channe ls.CoreChannel. SerializeBinar yMessage(IMessa ge msg, Stream outputStream, Boolean
includeVersions )
at
System.Runtime. Remoting.Channe ls.BinaryClient FormatterSink. SerializeMessag e(IMessage msg, ITransportHeade rs& headers, Stream& stream)
at
System.Runtime. Remoting.Channe ls.BinaryClient FormatterSink. SyncProcessMess age(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

.

Nov 15 '05 #10

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

Similar topics

1
2946
by: Michael | last post by:
Hi I anyone have a clue or can solve my problem I would be glad :-) Regards Michael I have a problem with creating an XML-document where the returning data from the webservice, have been serialized. In my program I am calling a webservice which then return the data to the calling program. The program then have to serialize the data and create an XML-document.
3
2160
by: CLEAR-RCIC | last post by:
Hi, I have a class that I am trying to serialize. To simplify, I've changed the names to Baseball, Teams and Players. Here is what the XML should look like: <BaseBall> <Teams> <Team> <Name /> <City />
14
14297
by: vince | last post by:
Can I add (append) to an xml file that already contains a serialized object, and be able to deserialize to either or both objects from the same file...??? How is this done...?? thanks, vince
5
24704
by: David Sworder | last post by:
Hi, I've created a UserControl-derived class called MyUserControl that is able to persist and subsequently reload its state. It exposes two methods as follows: public void Serialize(Stream s); public void Deserialize(Stream s); Within the MyUserControl class, there is a field of type MyInnerClass
3
10381
by: MAY | last post by:
Hi, I have a problem about serialize the form controls. I wrote a test program to test serialize a from but fail (->An unhandled exception of type 'System.Runtime.Serialization.SerializationException' occurred in mscorlib.dll) . Thx in advance. Here is the part of the code: Regards MAY
3
2384
by: Jerry | last post by:
Hi, I have a class like the following: class A { private B _b; A (B b) { _b = b; } ...
2
8881
by: films | last post by:
I understand the concept. Serialization of a class will add all the sub-objects of the class to the stream if there are also serializible. So say I have: class Author {
7
3540
by: Ben Amada | last post by:
I've created a class that I need to store in ViewState. However when I try to store it in ViewState, I get the following error: "The type 'solution.pe2' must be marked as Serializable or have a TypeConverter other than ReferenceConverter to be put in viewstate." I've included the <Serializable()> attribute, but I'm still getting the same error. The class is below ... as you can see it contains a Collection, two
4
7124
by: =?Utf-8?B?Qnlyb24=?= | last post by:
When I try to serialize an instance of the LocationCell below (note Building field) I get an error in the reflection attempt. If I remove the _Building field it serializes fine. I tried renaming Building._Name to Building._BName in case the duplicate name was the issue, but that didn't help. Is there a native way to serialize nested objects, or will I have to write my own? public class LocationCell
0
8585
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9057
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8934
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8780
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7603
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6441
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5800
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4534
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2947
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.