How to serialize a custom object aggregating other custom objects

為{幸葍}努か 提交于 2019-12-25 02:32:59

问题


I'm trying to serialize a custom object into a file. i've tried many thing but no one works, i must have missed a thing.

Here's the problem.

I have a singleton class i'm using to store my objects. Here is the code :

using DataLibrary.Model.Tests;
using System;
using System.Collections.Generic;
using System.IO;
using System.Runtime.Serialization;
using System.Xml.Serialization;
using testsPsychotechniques.View;

namespace testsPsychotechniques.Model
{
    [Serializable()]
    public class testSaver : ISerializable
    {
        private String lastName;
        private String firstName;
        private List<ITest> tests;


        public static testSaver Instance
        {
            get
            {
                return Nested.instance;
            }
        }

        public void addTest(ITest test)
        {
            tests.Add(test);
        }

        public Boolean save()
        {
            try
            {
                FileStream file = File.Open(".\\result.data",
                                            FileMode.Create,
                                            FileAccess.ReadWrite,
                                            FileShare.None);

                XmlSerializer serializer = new XmlSerializer(typeof(testSaver));
                serializer.Serialize(file, testSaver.Instance);
                file.Close();
                return true;
            }
            catch
            {
                return false;
            }
        }

        private testSaver()
        {
            this.firstName = Identification.firstName;
            this.lastName = Identification.lastName;
            this.tests = new List<ITest>();
        }


        private class Nested
        {
            internal static readonly testSaver instance = new testSaver();
        }

        public void GetObjectData(SerializationInfo info, StreamingContext context)
        {
            info.AddValue("lastName", this.lastName);
            info.AddValue("firstName", this.firstName);
            info.AddValue("testsResults", this.tests);
        }
    }
}

Now the real problem :

When i'm calling the save method, in the xml file generated i only have this data :

<?xml version="1.0"?>
<testSaver xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" />

The classes implementing the ITest interface are all marked serializable and have a method getObjectData.

Another hint is that the functions getObjectData are never used.

Thanks for any help


回答1:


XmlSerializer does not use the [Serializable] attribute or the ISerializable interface.

You have two options here:

  1. Continue to use XmlSerializer in which case you need public properties and a public noarg constructor.
  2. Change XmlSerializer to SoapFormatter and continue to use either [Serializable] or ISerializable, you don't need both.

It really depends what format you want your Xml in and whether you prefer attributes or code to control the serialization. I would go for option 1 as it is generally simpler.



来源:https://stackoverflow.com/questions/20836481/how-to-serialize-a-custom-object-aggregating-other-custom-objects

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!