1

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.

4
  • Show us the definition of usersObject, so we know how you handle users. It should probably be a list of userObject Commented Nov 15, 2013 at 10:37
  • Not sure I got it wright, but can't you just make the user object in your userObject class as a List<userObject>? Or better yet, instead of usersObject, directly create a List<userObject>. You can then add an XML serialization attribute specifying that you have a list. Commented Nov 15, 2013 at 10:37
  • Do you mean like having List<userObject> in your usersObject class? You can just do this and the serialiser will be able to deal with it Commented Nov 15, 2013 at 10:38
  • Actually I have the exact form of a XML file, and I'd liket to make an application that creats exactly the same xml. So the format is given: <users> <user> </user> <user> </user> ... </users> Commented Nov 15, 2013 at 10:53

2 Answers 2

1

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#.

Sign up to request clarification or add additional context in comments.

4 Comments

Yeah but I only need the user in many instance. If I create a list of Manifest the whole object will duplicate isn't it? (by the way thx for the advice the names :) )
@Mhorpheus Oh! I think I read your question all wrong. So you want to add more instances of "users" into your manifest, and not more manifests?
@Mhorpheus Updated my answer, it should now actually answer your question. Sorry for the misunderstanding.
I make the users object (just a container obj) and whitin I'd like to make a list of user obj, so the output should look like this: <users> <user> ... </user> <user> ... </user> </users> If I change the code to: users = new usersObject { user = new List<userObject> { ... } } But if I try to use this way, I get the following error for every property in the userObject: 'System.Collection.Generic.List < XMLproject.userObject>' does not contain a definition for... Maybe it is some permission failure?
0

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. :)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.