Serialize Class To XML

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Dan

    Serialize Class To XML

    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

  • Bob Powell [MVP]

    #2
    Re: Serialize Class To XML

    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


    Answer those GDI+ questions with the GDI+ FAQ


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

    "Dan" <ddeluca@fnisol utions.com> wrote in message
    news:220001c3a9 26$ab0e7a50$a60 1280a@phx.gbl.. .[color=blue]
    > 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
    >[/color]


    Comment

    • Nicholas Paldino [.NET/C# MVP]

      #3
      Re: Serialize Class To XML

      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]
      - mvp@spam.guard. caspershouse.co m

      "Dan" <ddeluca@fnisol utions.com> wrote in message
      news:220001c3a9 26$ab0e7a50$a60 1280a@phx.gbl.. .[color=blue]
      > 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
      >[/color]


      Comment

      • Matt Burland

        #4
        Re: Serialize Class To XML

        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" <ddeluca@fnisol utions.com> wrote in message
        news:220001c3a9 26$ab0e7a50$a60 1280a@phx.gbl.. .[color=blue]
        > 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
        >[/color]


        Comment

        • Dan

          #5
          Re: Serialize Class To XML

          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

          [color=blue]
          >-----Original Message-----
          >The exception tells you the problem You haven't marked[/color]
          the object you're[color=blue]
          >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" <ddeluca@fnisol utions.com> wrote in message
          >news:220001c3a 926$ab0e7a50$a6 01280a@phx.gbl. ..[color=green]
          >> All I Am Attempting To Serialize An Object To An XML[/color][/color]
          File.[color=blue][color=green]
          >> 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()[/color][/color]
          + "config.xml ";[color=blue][color=green]
          >> 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
          >>[/color][/color]
          System.Runtime. Serialization.F ormatterService s.InternalGetS[color=blue][color=green]
          >> erializableMemb ers(RuntimeType type, Boolean
          >> excludeNonSeria lizable)
          >> at
          >>[/color][/color]
          System.Runtime. Serialization.F ormatterService s.GetSerializa[color=blue][color=green]
          >> bleMembers(Type type, StreamingContex t context)
          >> at
          >>[/color][/color]
          System.Runtime. Serialization.F ormatters.Binar y.WriteObjectI[color=blue][color=green]
          >> nfo.InitMemberI nfo()
          >> at
          >>[/color][/color]
          System.Runtime. Serialization.F ormatters.Binar y.WriteObjectI[color=blue][color=green]
          >> nfo.InitSeriali ze(Object obj, ISurrogateSelec tor
          >> surrogateSelect or, StreamingContex t context,
          >> SerObjectInfoIn it serObjectInfoIn it, IFormatterConve rter
          >> converter)
          >> at
          >>[/color][/color]
          System.Runtime. Serialization.F ormatters.Binar y.WriteObjectI[color=blue][color=green]
          >> nfo.Serialize(O bject obj, ISurrogateSelec tor
          >> surrogateSelect or, StreamingContex t context,
          >> SerObjectInfoIn it serObjectInfoIn it, IFormatterConve rter
          >> converter)
          >> at
          >>[/color][/color]
          System.Runtime. Serialization.F ormatters.Binar y.ObjectWriter[color=blue][color=green]
          >> .Serialize(Obje ct graph, Header[] inHeaders,
          >> __BinaryWriter serWriter, Boolean fCheck)
          >> at
          >>[/color][/color]
          System.Runtime. Serialization.F ormatters.Binar y.BinaryFormat[color=blue][color=green]
          >> ter.Serialize(S tream serializationSt ream, Object graph,
          >> Header[] headers, Boolean fCheck)
          >> at
          >>[/color][/color]
          System.Runtime. Serialization.F ormatters.Binar y.BinaryFormat[color=blue][color=green]
          >> ter.Serialize(S tream serializationSt ream, Object graph,
          >> Header[] headers)
          >> at
          >>[/color][/color]
          System.Runtime. Remoting.Channe ls.CoreChannel. SerializeBinar[color=blue][color=green]
          >> yMessage(IMessa ge msg, Stream outputStream, Boolean
          >> includeVersions )
          >> at
          >>[/color][/color]
          System.Runtime. Remoting.Channe ls.BinaryClient FormatterSink.[color=blue][color=green]
          >> SerializeMessag e(IMessage msg, ITransportHeade rs&[/color][/color]
          headers,[color=blue][color=green]
          >> Stream& stream)
          >> at
          >>[/color][/color]
          System.Runtime. Remoting.Channe ls.BinaryClient FormatterSink.[color=blue][color=green]
          >> 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
          >>[/color]
          >
          >
          >.
          >[/color]

          Comment

          • Nicholas Paldino [.NET/C# MVP]

            #6
            Re: Serialize Class To XML

            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]
            - mvp@spam.guard. caspershouse.co m

            "Dan" <anonymous@disc ussions.microso ft.com> wrote in message
            news:22a801c3a9 2b$f794fc50$a60 1280a@phx.gbl.. .[color=blue]
            > 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
            >
            >[color=green]
            > >-----Original Message-----
            > >The exception tells you the problem You haven't marked[/color]
            > the object you're[color=green]
            > >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" <ddeluca@fnisol utions.com> wrote in message
            > >news:220001c3a 926$ab0e7a50$a6 01280a@phx.gbl. ..[color=darkred]
            > >> All I Am Attempting To Serialize An Object To An XML[/color][/color]
            > File.[color=green][color=darkred]
            > >> 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()[/color][/color]
            > + "config.xml ";[color=green][color=darkred]
            > >> 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
            > >>[/color][/color]
            > System.Runtime. Serialization.F ormatterService s.InternalGetS[color=green][color=darkred]
            > >> erializableMemb ers(RuntimeType type, Boolean
            > >> excludeNonSeria lizable)
            > >> at
            > >>[/color][/color]
            > System.Runtime. Serialization.F ormatterService s.GetSerializa[color=green][color=darkred]
            > >> bleMembers(Type type, StreamingContex t context)
            > >> at
            > >>[/color][/color]
            > System.Runtime. Serialization.F ormatters.Binar y.WriteObjectI[color=green][color=darkred]
            > >> nfo.InitMemberI nfo()
            > >> at
            > >>[/color][/color]
            > System.Runtime. Serialization.F ormatters.Binar y.WriteObjectI[color=green][color=darkred]
            > >> nfo.InitSeriali ze(Object obj, ISurrogateSelec tor
            > >> surrogateSelect or, StreamingContex t context,
            > >> SerObjectInfoIn it serObjectInfoIn it, IFormatterConve rter
            > >> converter)
            > >> at
            > >>[/color][/color]
            > System.Runtime. Serialization.F ormatters.Binar y.WriteObjectI[color=green][color=darkred]
            > >> nfo.Serialize(O bject obj, ISurrogateSelec tor
            > >> surrogateSelect or, StreamingContex t context,
            > >> SerObjectInfoIn it serObjectInfoIn it, IFormatterConve rter
            > >> converter)
            > >> at
            > >>[/color][/color]
            > System.Runtime. Serialization.F ormatters.Binar y.ObjectWriter[color=green][color=darkred]
            > >> .Serialize(Obje ct graph, Header[] inHeaders,
            > >> __BinaryWriter serWriter, Boolean fCheck)
            > >> at
            > >>[/color][/color]
            > System.Runtime. Serialization.F ormatters.Binar y.BinaryFormat[color=green][color=darkred]
            > >> ter.Serialize(S tream serializationSt ream, Object graph,
            > >> Header[] headers, Boolean fCheck)
            > >> at
            > >>[/color][/color]
            > System.Runtime. Serialization.F ormatters.Binar y.BinaryFormat[color=green][color=darkred]
            > >> ter.Serialize(S tream serializationSt ream, Object graph,
            > >> Header[] headers)
            > >> at
            > >>[/color][/color]
            > System.Runtime. Remoting.Channe ls.CoreChannel. SerializeBinar[color=green][color=darkred]
            > >> yMessage(IMessa ge msg, Stream outputStream, Boolean
            > >> includeVersions )
            > >> at
            > >>[/color][/color]
            > System.Runtime. Remoting.Channe ls.BinaryClient FormatterSink.[color=green][color=darkred]
            > >> SerializeMessag e(IMessage msg, ITransportHeade rs&[/color][/color]
            > headers,[color=green][color=darkred]
            > >> Stream& stream)
            > >> at
            > >>[/color][/color]
            > System.Runtime. Remoting.Channe ls.BinaryClient FormatterSink.[color=green][color=darkred]
            > >> 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
            > >>[/color]
            > >
            > >
            > >.
            > >[/color][/color]


            Comment

            • Guest's Avatar

              #7
              Re: Serialize Class To XML

              ms-help://MS.VSCC.2003/MS.MSDNQTR.2003 FEB.1033/cpguide/html/cpconintroducin g
              xmlserializatio n.htm


              and





              "Dan" <anonymous@disc ussions.microso ft.com> wrote in message
              news:22a801c3a9 2b$f794fc50$a60 1280a@phx.gbl.. .[color=blue]
              > 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
              >
              >[color=green]
              > >-----Original Message-----
              > >The exception tells you the problem You haven't marked[/color]
              > the object you're[color=green]
              > >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" <ddeluca@fnisol utions.com> wrote in message
              > >news:220001c3a 926$ab0e7a50$a6 01280a@phx.gbl. ..[color=darkred]
              > >> All I Am Attempting To Serialize An Object To An XML[/color][/color]
              > File.[color=green][color=darkred]
              > >> 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()[/color][/color]
              > + "config.xml ";[color=green][color=darkred]
              > >> 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
              > >>[/color][/color]
              > System.Runtime. Serialization.F ormatterService s.InternalGetS[color=green][color=darkred]
              > >> erializableMemb ers(RuntimeType type, Boolean
              > >> excludeNonSeria lizable)
              > >> at
              > >>[/color][/color]
              > System.Runtime. Serialization.F ormatterService s.GetSerializa[color=green][color=darkred]
              > >> bleMembers(Type type, StreamingContex t context)
              > >> at
              > >>[/color][/color]
              > System.Runtime. Serialization.F ormatters.Binar y.WriteObjectI[color=green][color=darkred]
              > >> nfo.InitMemberI nfo()
              > >> at
              > >>[/color][/color]
              > System.Runtime. Serialization.F ormatters.Binar y.WriteObjectI[color=green][color=darkred]
              > >> nfo.InitSeriali ze(Object obj, ISurrogateSelec tor
              > >> surrogateSelect or, StreamingContex t context,
              > >> SerObjectInfoIn it serObjectInfoIn it, IFormatterConve rter
              > >> converter)
              > >> at
              > >>[/color][/color]
              > System.Runtime. Serialization.F ormatters.Binar y.WriteObjectI[color=green][color=darkred]
              > >> nfo.Serialize(O bject obj, ISurrogateSelec tor
              > >> surrogateSelect or, StreamingContex t context,
              > >> SerObjectInfoIn it serObjectInfoIn it, IFormatterConve rter
              > >> converter)
              > >> at
              > >>[/color][/color]
              > System.Runtime. Serialization.F ormatters.Binar y.ObjectWriter[color=green][color=darkred]
              > >> .Serialize(Obje ct graph, Header[] inHeaders,
              > >> __BinaryWriter serWriter, Boolean fCheck)
              > >> at
              > >>[/color][/color]
              > System.Runtime. Serialization.F ormatters.Binar y.BinaryFormat[color=green][color=darkred]
              > >> ter.Serialize(S tream serializationSt ream, Object graph,
              > >> Header[] headers, Boolean fCheck)
              > >> at
              > >>[/color][/color]
              > System.Runtime. Serialization.F ormatters.Binar y.BinaryFormat[color=green][color=darkred]
              > >> ter.Serialize(S tream serializationSt ream, Object graph,
              > >> Header[] headers)
              > >> at
              > >>[/color][/color]
              > System.Runtime. Remoting.Channe ls.CoreChannel. SerializeBinar[color=green][color=darkred]
              > >> yMessage(IMessa ge msg, Stream outputStream, Boolean
              > >> includeVersions )
              > >> at
              > >>[/color][/color]
              > System.Runtime. Remoting.Channe ls.BinaryClient FormatterSink.[color=green][color=darkred]
              > >> SerializeMessag e(IMessage msg, ITransportHeade rs&[/color][/color]
              > headers,[color=green][color=darkred]
              > >> Stream& stream)
              > >> at
              > >>[/color][/color]
              > System.Runtime. Remoting.Channe ls.BinaryClient FormatterSink.[color=green][color=darkred]
              > >> 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
              > >>[/color]
              > >
              > >
              > >.
              > >[/color][/color]


              Comment

              • Dan

                #8
                Re: Serialize Class To XML

                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
                [color=blue]
                >-----Original Message-----
                > I assume your class is marked as Serializable, but you[/color]
                also need to check[color=blue]
                >that every object it contains is also either marked as[/color]
                Serializable itself[color=blue]
                >or marked as NonSerialized in your class.
                >
                >
                >"Dan" <ddeluca@fnisol utions.com> wrote in message
                >news:220001c3a 926$ab0e7a50$a6 01280a@phx.gbl. ..[color=green]
                >> All I Am Attempting To Serialize An Object To An XML[/color][/color]
                File.[color=blue][color=green]
                >> 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()[/color][/color]
                + "config.xml ";[color=blue][color=green]
                >> 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
                >>[/color][/color]
                System.Runtime. Serialization.F ormatterService s.InternalGetS[color=blue][color=green]
                >> erializableMemb ers(RuntimeType type, Boolean
                >> excludeNonSeria lizable)
                >> at
                >>[/color][/color]
                System.Runtime. Serialization.F ormatterService s.GetSerializa[color=blue][color=green]
                >> bleMembers(Type type, StreamingContex t context)
                >> at
                >>[/color][/color]
                System.Runtime. Serialization.F ormatters.Binar y.WriteObjectI[color=blue][color=green]
                >> nfo.InitMemberI nfo()
                >> at
                >>[/color][/color]
                System.Runtime. Serialization.F ormatters.Binar y.WriteObjectI[color=blue][color=green]
                >> nfo.InitSeriali ze(Object obj, ISurrogateSelec tor
                >> surrogateSelect or, StreamingContex t context,
                >> SerObjectInfoIn it serObjectInfoIn it, IFormatterConve rter
                >> converter)
                >> at
                >>[/color][/color]
                System.Runtime. Serialization.F ormatters.Binar y.WriteObjectI[color=blue][color=green]
                >> nfo.Serialize(O bject obj, ISurrogateSelec tor
                >> surrogateSelect or, StreamingContex t context,
                >> SerObjectInfoIn it serObjectInfoIn it, IFormatterConve rter
                >> converter)
                >> at
                >>[/color][/color]
                System.Runtime. Serialization.F ormatters.Binar y.ObjectWriter[color=blue][color=green]
                >> .Serialize(Obje ct graph, Header[] inHeaders,
                >> __BinaryWriter serWriter, Boolean fCheck)
                >> at
                >>[/color][/color]
                System.Runtime. Serialization.F ormatters.Binar y.BinaryFormat[color=blue][color=green]
                >> ter.Serialize(S tream serializationSt ream, Object graph,
                >> Header[] headers, Boolean fCheck)
                >> at
                >>[/color][/color]
                System.Runtime. Serialization.F ormatters.Binar y.BinaryFormat[color=blue][color=green]
                >> ter.Serialize(S tream serializationSt ream, Object graph,
                >> Header[] headers)
                >> at
                >>[/color][/color]
                System.Runtime. Remoting.Channe ls.CoreChannel. SerializeBinar[color=blue][color=green]
                >> yMessage(IMessa ge msg, Stream outputStream, Boolean
                >> includeVersions )
                >> at
                >>[/color][/color]
                System.Runtime. Remoting.Channe ls.BinaryClient FormatterSink.[color=blue][color=green]
                >> SerializeMessag e(IMessage msg, ITransportHeade rs&[/color][/color]
                headers,[color=blue][color=green]
                >> Stream& stream)
                >> at
                >>[/color][/color]
                System.Runtime. Remoting.Channe ls.BinaryClient FormatterSink.[color=blue][color=green]
                >> 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
                >>[/color]
                >
                >
                >.
                >[/color]

                Comment

                • Matt Burland

                  #9
                  Re: Serialize Class To XML

                  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" <anonymous@disc ussions.microso ft.com> wrote in message
                  news:070901c3a9 3a$8ce549a0$a50 1280a@phx.gbl.. .[color=blue]
                  > 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
                  >[color=green]
                  > >-----Original Message-----
                  > > I assume your class is marked as Serializable, but you[/color]
                  > also need to check[color=green]
                  > >that every object it contains is also either marked as[/color]
                  > Serializable itself[color=green]
                  > >or marked as NonSerialized in your class.
                  > >
                  > >
                  > >"Dan" <ddeluca@fnisol utions.com> wrote in message
                  > >news:220001c3a 926$ab0e7a50$a6 01280a@phx.gbl. ..[color=darkred]
                  > >> All I Am Attempting To Serialize An Object To An XML[/color][/color]
                  > File.[color=green][color=darkred]
                  > >> 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()[/color][/color]
                  > + "config.xml ";[color=green][color=darkred]
                  > >> 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
                  > >>[/color][/color]
                  > System.Runtime. Serialization.F ormatterService s.InternalGetS[color=green][color=darkred]
                  > >> erializableMemb ers(RuntimeType type, Boolean
                  > >> excludeNonSeria lizable)
                  > >> at
                  > >>[/color][/color]
                  > System.Runtime. Serialization.F ormatterService s.GetSerializa[color=green][color=darkred]
                  > >> bleMembers(Type type, StreamingContex t context)
                  > >> at
                  > >>[/color][/color]
                  > System.Runtime. Serialization.F ormatters.Binar y.WriteObjectI[color=green][color=darkred]
                  > >> nfo.InitMemberI nfo()
                  > >> at
                  > >>[/color][/color]
                  > System.Runtime. Serialization.F ormatters.Binar y.WriteObjectI[color=green][color=darkred]
                  > >> nfo.InitSeriali ze(Object obj, ISurrogateSelec tor
                  > >> surrogateSelect or, StreamingContex t context,
                  > >> SerObjectInfoIn it serObjectInfoIn it, IFormatterConve rter
                  > >> converter)
                  > >> at
                  > >>[/color][/color]
                  > System.Runtime. Serialization.F ormatters.Binar y.WriteObjectI[color=green][color=darkred]
                  > >> nfo.Serialize(O bject obj, ISurrogateSelec tor
                  > >> surrogateSelect or, StreamingContex t context,
                  > >> SerObjectInfoIn it serObjectInfoIn it, IFormatterConve rter
                  > >> converter)
                  > >> at
                  > >>[/color][/color]
                  > System.Runtime. Serialization.F ormatters.Binar y.ObjectWriter[color=green][color=darkred]
                  > >> .Serialize(Obje ct graph, Header[] inHeaders,
                  > >> __BinaryWriter serWriter, Boolean fCheck)
                  > >> at
                  > >>[/color][/color]
                  > System.Runtime. Serialization.F ormatters.Binar y.BinaryFormat[color=green][color=darkred]
                  > >> ter.Serialize(S tream serializationSt ream, Object graph,
                  > >> Header[] headers, Boolean fCheck)
                  > >> at
                  > >>[/color][/color]
                  > System.Runtime. Serialization.F ormatters.Binar y.BinaryFormat[color=green][color=darkred]
                  > >> ter.Serialize(S tream serializationSt ream, Object graph,
                  > >> Header[] headers)
                  > >> at
                  > >>[/color][/color]
                  > System.Runtime. Remoting.Channe ls.CoreChannel. SerializeBinar[color=green][color=darkred]
                  > >> yMessage(IMessa ge msg, Stream outputStream, Boolean
                  > >> includeVersions )
                  > >> at
                  > >>[/color][/color]
                  > System.Runtime. Remoting.Channe ls.BinaryClient FormatterSink.[color=green][color=darkred]
                  > >> SerializeMessag e(IMessage msg, ITransportHeade rs&[/color][/color]
                  > headers,[color=green][color=darkred]
                  > >> Stream& stream)
                  > >> at
                  > >>[/color][/color]
                  > System.Runtime. Remoting.Channe ls.BinaryClient FormatterSink.[color=green][color=darkred]
                  > >> 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
                  > >>[/color]
                  > >
                  > >
                  > >.
                  > >[/color][/color]


                  Comment

                  • Bob Powell [MVP]

                    #10
                    Re: Serialize Class To XML

                    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


                    Answer those GDI+ questions with the GDI+ FAQ


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

                    "Dan" <anonymous@disc ussions.microso ft.com> wrote in message
                    news:22a801c3a9 2b$f794fc50$a60 1280a@phx.gbl.. .[color=blue]
                    > 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
                    >
                    >[color=green]
                    > >-----Original Message-----
                    > >The exception tells you the problem You haven't marked[/color]
                    > the object you're[color=green]
                    > >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" <ddeluca@fnisol utions.com> wrote in message
                    > >news:220001c3a 926$ab0e7a50$a6 01280a@phx.gbl. ..[color=darkred]
                    > >> All I Am Attempting To Serialize An Object To An XML[/color][/color]
                    > File.[color=green][color=darkred]
                    > >> 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()[/color][/color]
                    > + "config.xml ";[color=green][color=darkred]
                    > >> 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
                    > >>[/color][/color]
                    > System.Runtime. Serialization.F ormatterService s.InternalGetS[color=green][color=darkred]
                    > >> erializableMemb ers(RuntimeType type, Boolean
                    > >> excludeNonSeria lizable)
                    > >> at
                    > >>[/color][/color]
                    > System.Runtime. Serialization.F ormatterService s.GetSerializa[color=green][color=darkred]
                    > >> bleMembers(Type type, StreamingContex t context)
                    > >> at
                    > >>[/color][/color]
                    > System.Runtime. Serialization.F ormatters.Binar y.WriteObjectI[color=green][color=darkred]
                    > >> nfo.InitMemberI nfo()
                    > >> at
                    > >>[/color][/color]
                    > System.Runtime. Serialization.F ormatters.Binar y.WriteObjectI[color=green][color=darkred]
                    > >> nfo.InitSeriali ze(Object obj, ISurrogateSelec tor
                    > >> surrogateSelect or, StreamingContex t context,
                    > >> SerObjectInfoIn it serObjectInfoIn it, IFormatterConve rter
                    > >> converter)
                    > >> at
                    > >>[/color][/color]
                    > System.Runtime. Serialization.F ormatters.Binar y.WriteObjectI[color=green][color=darkred]
                    > >> nfo.Serialize(O bject obj, ISurrogateSelec tor
                    > >> surrogateSelect or, StreamingContex t context,
                    > >> SerObjectInfoIn it serObjectInfoIn it, IFormatterConve rter
                    > >> converter)
                    > >> at
                    > >>[/color][/color]
                    > System.Runtime. Serialization.F ormatters.Binar y.ObjectWriter[color=green][color=darkred]
                    > >> .Serialize(Obje ct graph, Header[] inHeaders,
                    > >> __BinaryWriter serWriter, Boolean fCheck)
                    > >> at
                    > >>[/color][/color]
                    > System.Runtime. Serialization.F ormatters.Binar y.BinaryFormat[color=green][color=darkred]
                    > >> ter.Serialize(S tream serializationSt ream, Object graph,
                    > >> Header[] headers, Boolean fCheck)
                    > >> at
                    > >>[/color][/color]
                    > System.Runtime. Serialization.F ormatters.Binar y.BinaryFormat[color=green][color=darkred]
                    > >> ter.Serialize(S tream serializationSt ream, Object graph,
                    > >> Header[] headers)
                    > >> at
                    > >>[/color][/color]
                    > System.Runtime. Remoting.Channe ls.CoreChannel. SerializeBinar[color=green][color=darkred]
                    > >> yMessage(IMessa ge msg, Stream outputStream, Boolean
                    > >> includeVersions )
                    > >> at
                    > >>[/color][/color]
                    > System.Runtime. Remoting.Channe ls.BinaryClient FormatterSink.[color=green][color=darkred]
                    > >> SerializeMessag e(IMessage msg, ITransportHeade rs&[/color][/color]
                    > headers,[color=green][color=darkred]
                    > >> Stream& stream)
                    > >> at
                    > >>[/color][/color]
                    > System.Runtime. Remoting.Channe ls.BinaryClient FormatterSink.[color=green][color=darkred]
                    > >> 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
                    > >>[/color]
                    > >
                    > >
                    > >.
                    > >[/color][/color]


                    Comment

                    • Dan

                      #11
                      Serialize Class To XML

                      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

                      Comment

                      Working...