1

I have a class, lets call it, Employee, that has a list of qualifications as property

[XmlRoot("Employee")]
public class Employee
{
    private string name;
    private IListManager<string> qualification = new ListManager<string>();

    public Employee()
    {
    }

    [XmlElement("Name")]
    public string Name
    {
        get
        {
            return name;
        }
        set
        {
            if (value != null)
            {
                name = value;
            }
        }
    }

    public ListManager<string> QualificationList
    {
        get
        {
            return qualification as ListManager<string>;
        }
    }

    [XmlElement("Qual")]  
    public string Qualifications
    {
        get
        {
            return ReturnQualifications();
        }
    }

    private string ReturnQualifications()
    {
        string qual = string.Empty;
        for (int i = 0; i < qualification.Count; i++)
        {
            qual += " " + qualification.GetElement(i);
        }
        return qual;
    }

    public override String ToString()
    {
        String infFormat = String.Format("{0, 1} {1, 15}", this.name, Qualifications);
        return infFormat;
    }
}

}

Then I have a method that serializes the above class tom XML by taking a list of Employees as patameter, the method is generic:

public static void Serialize<T>(string path, T typeOf)
        {
            XmlSerializer xmlSer = new XmlSerializer(typeof(T));
            TextWriter t = new StreamWriter(path);
            try
            {
                xmlSer.Serialize(t, typeOf);
            }
            catch
            {
                throw;
            }
            finally
            {
                if (t != null)
                {
                    t.Close();
                }
            }
        }

This is how I call the method XMLSerialize:

controller.XMLSerialize<Employee>(opnXMLFileDialog.FileName, employeeList);

The result of the method execution returns a file and is shown below:

<?xml version="1.0" encoding="utf-8"?>
<ArrayOfEmployees xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <Employees>
    <Name>g</Name>
  </Employees>
</ArrayOfEmployees>

as you can see there is only the property name included in the file. How do I proceed from here and serialize the list of qualifications too? Any suggestionswill be greatly appreciated. Thanks in advance.

Mnemonics
  • 679
  • 1
  • 10
  • 26
  • This may be related: http://stackoverflow.com/questions/13401192/why-are-properties-without-a-setter-not-serialized – NoChance Dec 01 '14 at 18:52
  • Would you like to elaborate why you find that post to be related to mine? Do you mean that the reason my property is not serialized is because i don't have a setter? Even if it was the case, I still need to deserialize the file. Thank you for your contribution. – Mnemonics Dec 01 '14 at 18:56
  • You are correct, this is one aspect. I am not familiar with your serializer code. This is more like what I would do:http://stackoverflow.com/questions/6115721/how-to-save-restore-serializable-object-to-from-file – NoChance Dec 01 '14 at 19:07

1 Answers1

1

In order to serialize a property, XmlSerializer requires that the property have both a public setter and public getter. So whatever property you choose to serialize your qualifications must have a setter as well as a getter.

The obvious solution would be for your QualificationList to have a setter as well as a getter:

public ListManager<string> QualificationList
{
    get
    {
        return qualification as ListManager<string>;
    }
    set
    {
        qualification = value;
    }  
}

If for some reason this cannot be done -- perhaps because your ListManager<string> class is not serializable -- you could serialize and deserialize it as a proxy array of strings, like so:

    [XmlIgnore]
    public ListManager<string> QualificationList
    {
        get
        {
            return qualification as ListManager<string>;
        }
    }

    [XmlElement("Qual")]
    [Browsable(false), EditorBrowsable(EditorBrowsableState.Never)]
    [DebuggerBrowsable(DebuggerBrowsableState.Never)]
    public string[] QualificationArray
    {
        get
        {
            return Enumerable.Range(0, qualification.Count).Select(i => qualification.GetElement(i)).ToArray();
        }
        set
        {
            // Here I am assuming your ListManager<string> class has Clear() and Add() methods.
            qualification.Clear();
            if (value != null)
                foreach (var str in value)
                {
                    qualification.Add(str);
                }
        }
    }

Then for the following employee list:

        var employee = new Employee() { Name = "Mnemonics" };
        employee.QualificationList.Add("posts on stackoverflow");
        employee.QualificationList.Add("easy to remember");
        employee.QualificationList.Add("hard to spell");

        var list = new List<Employee>();
        list.Add(employee);

The following XML is generated:

<?xml version="1.0" encoding="utf-16"?>
<ArrayOfEmployee xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
    <Employee>
        <Name>Mnemonics</Name>
        <Qual>posts on stackoverflow</Qual>
        <Qual>easy to remember</Qual>
        <Qual>hard to spell</Qual>
    </Employee>
</ArrayOfEmployee>

Is that satisfactory?

dbc
  • 104,963
  • 20
  • 228
  • 340
  • 1
    I'm so sorry for late response. Yes it is, although, for security reasons, I'm not able to create a public property of the list. E.g other devs shouldn't have access to a list filled with users credentials. But you answer is very satisfactory – Mnemonics Dec 02 '14 at 16:10