I have xml input which looks like (simplified version used for example):
<Student>
<Subject> History </Subject>
<Subject> English </Subject>
</Student>
Is there a way to get the above xml deserialized to a object whose class looks like:
[Serializable]
[XmlRoot(ElementName = "Student", Namespace="")]
class Student
{
public Student()
{
Subject = new List<string>();
}
public List<string> Subject {get;set;}
}
Note I am trying to figure out if this can be done without having to implement IXmlSerializable interface, and I want to use a list to store the Subject values (not a string [] which I know is possible is I use the XmlElement attribute).
Decorate the Subject property with the XmlArrayAttribute.
If you need to omit the Subject element and have the Subject entries directly below Student, you can simply use the [XmlElement] attribute:
Serializing this with the Student class produces output similar to this: