简体   繁体   中英

Is it possible to xml-serialize a property (in a xml serialized class) that returns a list of string objects?

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.

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?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM