简体   繁体   English

C#类序列化

[英]C# class serializing

I'm developing a little application that generates an xml file form an object. 我正在开发一个小的应用程序,它可以从一个对象生成一个xml文件。 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. (用户对象必须包含更多的用户实例)我认为这是一件容易的事,我只是想不通。

To store an unknown number of instances of an object you can use a List . 要存储未知数量的对象实例,可以使用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. 序列化程序可以正确处理List,因此这些行没有任何更改。 You might also want to add a constructor that initializes the List to empty in your class : 您可能还需要添加一个构造函数,以将List初始化为类中的空列表:

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#. 附带说明一下,您应该考虑为类名和属性使用UpperCamelCase( manifest将变为Manifest ),这是C#中非常常见的约定。

So the XML form I want to create: 因此,我想创建XML表单:

<manifest>
 <meta>
 </meta>

 <option>
 </option>

 <datasource>
 </datasource>

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

To achive this I create the manifestObject: 为此,我创建了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: 而usersObject看起来像:

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. VS没有提供该类的任何字段,因此看不到它。 I really don't know what is wrong, and now there is a big mess in my head. 我真的不知道出什么问题了,现在我的头大乱了。 :) :)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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