简体   繁体   English

序列化对象数组

[英]serialize array of object

[Serializable()]
public class A
{

        [XmlArrayAttribute("Item")]
        public  List<B> items;
}

[Serializable()]
[XmlType(TypeName = "Item")]
public class B
{

}

After serialization, I found I have something like 序列化后,我发现我有类似的东西

<Item>
   <Item> **** </Item>
   <Item> **** </Item>
    *****
</item>

But I only want 但我只想要

 <Item> **** </Item>
 <Item> **** </Item>

How to get it? 怎么弄?

public class A
{
    [XmlElement("Item")]
    public List<B> items;
}

public class B
{

}

Notice that you don't need the [Serializable] attribute. 请注意,您不需要[Serializable]属性。 It is used only for binary serialization and ignored by XmlSerializer which is what I suspect you are using even if this should have been clearly stated in your question. 它仅用于二进制序列化并被XmlSerializer忽略,这是我怀疑你正在使用的,即使你的问题中已经明确说明了这一点。 Also for better encapsulation I would recommend you using properties instead of fields. 另外为了更好的封装,我建议你使用属性而不是字段。 And another remark: the standard naming convention in C# dictates that property names should start with a capital letter. 还有一句话:C#中的标准命名约定规定属性名称应以大写字母开头。

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

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