开发者

serialize array of object

开发者 https://www.devze.com 2023-01-25 06:35 出处:网络
[Serializable()] public class A { [XmlArr开发者_如何学PythonayAttribute(\"Item\")] publicList<B> items;
[Serializable()]
public class A
{

        [XmlArr开发者_如何学PythonayAttribute("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. 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. 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.

0

精彩评论

暂无评论...
验证码 换一张
取 消