Deserialize method that "loads" the class instance itself: how???

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

    Deserialize method that "loads" the class instance itself: how???

    Hello,

    I've got an xml stream that I'd need to deserialize into an instance of a
    given class A. I'd like to create an instance method on class A (method
    Deserialize) that takes this XML stream as input and deserializes it "into
    itself" ... in other words I'd like it to "fill" the instance of class A on
    which the method has been called instead of returning another instance of
    class A.

    The code below gives a good idea of what I'd like:

    public class A
    {
    public void Deserialize(Mem oryStream stream)
    {
    XmlSerializer serializer = new XmlSerializer(t ypeof(A));

    // the following line of code obviously does not work since this is
    readonly
    // but it gives a good idea of what I'd like to do
    this = serializer.Dese rialize(stream) ;
    }
    }

    How can I accomplish this without having to manually "load" all the class
    fields???


    Bob Rock



  • Dennis Myrén

    #2
    Re: Deserialize method that "loads&quo t; the class instance itself: how???

    I would implement a static method in the class that is serializable, like:

    public static YourClass Deserialize ( Stream fromStream )
    {
    XmlSerializer serializer = new XmlSerializer(t ypeof(YourClass ));
    return (YourClass) serializer.Dese rialize(stream) ;
    }


    "Bob Rock" <nospam.yet_ano ther_apprentice @hotmail.com> wrote in message
    news:ug6R%23qaO EHA.3052@TK2MSF TNGP12.phx.gbl. ..[color=blue]
    > Hello,
    >
    > I've got an xml stream that I'd need to deserialize into an instance of a
    > given class A. I'd like to create an instance method on class A (method
    > Deserialize) that takes this XML stream as input and deserializes it "into
    > itself" ... in other words I'd like it to "fill" the instance of class A[/color]
    on[color=blue]
    > which the method has been called instead of returning another instance of
    > class A.
    >
    > The code below gives a good idea of what I'd like:
    >
    > public class A
    > {
    > public void Deserialize(Mem oryStream stream)
    > {
    > XmlSerializer serializer = new XmlSerializer(t ypeof(A));
    >
    > // the following line of code obviously does not work since this[/color]
    is[color=blue]
    > readonly
    > // but it gives a good idea of what I'd like to do
    > this = serializer.Dese rialize(stream) ;
    > }
    > }
    >
    > How can I accomplish this without having to manually "load" all the class
    > fields???
    >
    >
    > Bob Rock
    >
    >
    >[/color]


    Comment

    • Bob Rock

      #3
      Re: Deserialize method that &quot;loads&quo t; the class instance itself: how???

      "Dennis Myrén" <dennis@oslokb. no> wrote in message
      news:AB2pc.1462 $RL3.32889@news 2.e.nsc.no...[color=blue]
      > I would implement a static method in the class that is serializable, like:
      >
      > public static YourClass Deserialize ( Stream fromStream )
      > {
      > XmlSerializer serializer = new XmlSerializer(t ypeof(YourClass ));
      > return (YourClass) serializer.Dese rialize(stream) ;
      > }[/color]

      Dannis, that is what I will do if I can't find an easy way to do it with an
      instance method.

      Bob Rock



      Comment

      • Dennis Myrén

        #4
        Re: Deserialize method that &quot;loads&quo t; the class instance itself: how???

        Well, you can do it with an instance method by deserializing the XML
        to a new instance of the class, and than copy all properties of that
        instance to your actual instance, like;

        public void LoadState ( Stream fromStream )
        {
        YourClass c = (YourClass) new
        XmlSerializer(t ypeof(YourClass )).Deserialize( stream);
        this.aProperty = c.aProperty;
        this.someOtherP roperty = c.someOtherProp erty;
        c = null;
        }

        Regards, Dennis

        "Bob Rock" <nospam.yet_ano ther_apprentice @hotmail.com> wrote in message
        news:eyYzFJbOEH A.2944@TK2MSFTN GP10.phx.gbl...[color=blue]
        > "Dennis Myrén" <dennis@oslokb. no> wrote in message
        > news:AB2pc.1462 $RL3.32889@news 2.e.nsc.no...[color=green]
        > > I would implement a static method in the class that is serializable,[/color][/color]
        like:[color=blue][color=green]
        > >
        > > public static YourClass Deserialize ( Stream fromStream )
        > > {
        > > XmlSerializer serializer = new XmlSerializer(t ypeof(YourClass ));
        > > return (YourClass) serializer.Dese rialize(stream) ;
        > > }[/color]
        >
        > Dannis, that is what I will do if I can't find an easy way to do it with[/color]
        an[color=blue]
        > instance method.
        >
        > Bob Rock
        >
        >
        >[/color]


        Comment

        • Nicholas Paldino [.NET/C# MVP]

          #5
          Re: Deserialize method that &quot;loads&quo t; the class instance itself: how???

          Bob,

          Why not do this. Get the schema for the XML and then run the XSD.exe
          tool against it. This will create a C# file which is a class (which might
          derive from DataSet if you say so) that you can compile in your app. Then,
          you can use the ReadXml method on the DataSet (if you choose to have your
          class derive from that), or the Deserialize method on the XmlSerializer
          class to deserialize an instance of the class in your app.

          Once you have that, you can do anything you want with the class, or have
          a containing class use it for whatever purposes you wish.

          Hope this helps.



          "Bob Rock" <nospam.yet_ano ther_apprentice @hotmail.com> wrote in message
          news:eyYzFJbOEH A.2944@TK2MSFTN GP10.phx.gbl...[color=blue]
          > "Dennis Myrén" <dennis@oslokb. no> wrote in message
          > news:AB2pc.1462 $RL3.32889@news 2.e.nsc.no...[color=green]
          > > I would implement a static method in the class that is serializable,[/color][/color]
          like:[color=blue][color=green]
          > >
          > > public static YourClass Deserialize ( Stream fromStream )
          > > {
          > > XmlSerializer serializer = new XmlSerializer(t ypeof(YourClass ));
          > > return (YourClass) serializer.Dese rialize(stream) ;
          > > }[/color]
          >
          > Dannis, that is what I will do if I can't find an easy way to do it with[/color]
          an[color=blue]
          > instance method.
          >
          > Bob Rock
          >
          >
          >[/color]


          Comment

          Working...