Results 1 to 5 of 5

Thread: [RESOLVED] XML Serialization question

  1. #1

    Thread Starter
    Frenzied Member tr333's Avatar
    Join Date
    Nov 2004
    Location
    /dev/st0
    Posts
    1,605

    Resolved [RESOLVED] XML Serialization question

    i have a class that contains a few properties and two methods, SaveToFile and LoadFromFile.

    if i serialize the class in SaveToFile using serializer.Serialize(fs, this); is it possible to deserialize in the same way without creating a temporary variable to deserialize to and then setting the properties from the temporary variable?
    CSS layout comes in to the 21st century with flexbox!
    Just another Perl hacker,

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: XML Serialization question

    What do you mean by a temporary variable? Deserialising creates an object of the specified type, so you just use that object.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    Frenzied Member tr333's Avatar
    Join Date
    Nov 2004
    Location
    /dev/st0
    Posts
    1,605

    Re: XML Serialization question

    i have a class like the following testclass:
    Code:
    class testclass
    {
        string _tmp1;
        string _tmp2;
    
        public string Property1
        {
             get { return _tmp1; }
             set { _tmp1 = value; }
        }
    
        public string Property2
        {
            get { return _tmp2; }
            set { _tmp2 = value; }
        }
    
        void SaveToFile()
        {
            FileStream fs = new FileStream("tmp.xml", FileMode.Create, FileAccess.Write);
             XmlSerializer xmls = new XmlSerializer(typeof(testclass));
             xmls.Serialize(fs, this);
             fs.Close();
        }
    }
    i would like to add a method "LoadFromFile" to the class testclass that is basically the reverse of SaveToFile.
    i can't just do "this = xmls.Deserialize(fs);". is there a better way of doing this other than doing:
    Code:
    testclass tmp;
    tmp = xmls.Deserialize(fs);
    this._tmp1 = tmp.Property1;
    this._tmp2 = tmp.Property2;
    this method works, but is very time consuming if there is a lot of properties.
    CSS layout comes in to the 21st century with flexbox!
    Just another Perl hacker,

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: XML Serialization question

    I've done a similar thing in the past and I made the Save member an public instance method that returned a bool and the Load member a public static method that returned an instance of the class. You would then do something like this to deserialise a file:
    Code:
    TestClass myTestClass = TestClass.Load(fileName);
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  5. #5

    Thread Starter
    Frenzied Member tr333's Avatar
    Join Date
    Nov 2004
    Location
    /dev/st0
    Posts
    1,605

    Re: XML Serialization question

    great! thanks.
    CSS layout comes in to the 21st century with flexbox!
    Just another Perl hacker,

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width