I'm developing a little application that generates an xml file form an object. I've read articles and many other thing in the topic, and so far everything went well. However when I'd like to initialize more instance of the same type, I just can't do that.
Here is the base class:
public class manifest
{
public metaObject meta { get; set; }
public optionsObject options { get; set; }
public datasourcesObject datasources { get; set; }
public usersObject users { get; set; }
}
I can make the object well, I can add some data as well:
manifest manifestobjektum = new manifest
{
meta = new metaObject
{
... // it's OK
},
options = new optionsObject
{
... // it's OK
},
datasources = new datasourcesObject
{
.. // It's OK
},
users = new usersObject
{
user = new userObject
{
.. // it's OK
}
}
};
XmlSerializer serializer = new XmlSerializer(typeof(manifest));
serializer.Serialize(File.Create("testXMLfaszomat.xml"), manifestobjektum);
And now the question: I'd like to create more user object (don't know how much), how shall I modify the code to achive this? (the users object have to contain more instance of user) I think it is some easy thing, I just can't figure it out.
userobject in youruserObjectclass as aList<userObject>? Or better yet, instead ofusersObject, directly create aList<userObject>. You can then add an XML serialization attribute specifying that you have a list.List<userObject>in yourusersObjectclass? You can just do this and the serialiser will be able to deal with it