4
[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?

1 Answer 1

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

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

2 Comments

Thanks. When should I use XmlArrayAttribute?
When you want to generate and personalize a nested sequence of XML elements from that member.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.