SecThor
SecThor

Reputation: 69

C# class serializing

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.

Upvotes: 1

Views: 132

Answers (2)

SecThor
SecThor

Reputation: 69

So the XML form I want to create:

<manifest>
 <meta>
 </meta>

 <option>
 </option>

 <datasource>
 </datasource>

 <users>
   <user>
   </user>
...
   <user>
   </user>
 </users>
</manifest>

To achive this I create the manifestObject:

public class manifest
{
    public metaObject meta { get; set; }
    public optionsObject options { get; set; }
    public datasourcesObject datasources { get; set; }

    public usersObject users { get; set; }
}

And the usersObject looks like:

public class usersObject
{
    public List<userObject> user { get; set; }
}

But when I try to fill data to the class:

users = new usersObject
{
  user = new List<userObject>
  {

   }
}

the VS doesn't offer any field from the class, so it doesn't see it. I really don't know what is wrong, and now there is a big mess in my head. :)

Upvotes: 0

Pierre-Luc Pineault
Pierre-Luc Pineault

Reputation: 9191

To store an unknown number of instances of an object you can use a List.

So to add more users, your class would become :

public class manifest
{
    public metaObject meta { get; set; }
    public optionsObject options { get; set; }
    public datasourcesObject datasources { get; set; }
    public List<usersObject> users { get; set; }        
}

and you can change the initialization to something like this :

users = new List<usersObject>
{
    new userObject(),
    new userObject(),
    new userObject()
}

The serializer can handle List correctly, so there's nothing to change about those lines. You might also want to add a constructor that initializes the List to empty in your class :

public manifest()
{
    user = new List<userObject>
}

so you can add users later without doing it explicitly in your class initialization. For example, this would now work :

manifest someManifest = new manifest();
someManifest.users.Add(new userObject());


As a side note, you should consider using UpperCamelCase for your class names and properties (manifest would become Manifest), it's a pretty common convention in C#.

Upvotes: 1

Related Questions