4

I have two serializable objects:

[Serializable]
public class A
{
    [XmlArray("Items")]
    [XmlArrayItem]
    public UnitItem[] Items{get;set;}

    ......
}

[Serializable]
public class UnitItem
{
    [XmlAttribute]
    public string Name { get; set; }

    [XmlText]
    public string Data { get; set; }
}

I can serialize A like:

<Items>
   <UnitItem Name="AAA">balabala</UnitItem>
   <UnitItem Name="BBB">bulubulu</UnitItem>
   ......
</Items>

but now I want to create a XML structure as follows:

<Items>
    <AAA>balabala</AAA>
    <BBB>bulubulu</BBB>
   ......
</Items>

that is to say i expected the ElementName of XmlArrayItem is member variable's value not variable's name. How can i do ?

If i have to inherit ISerializable could you tell me how to implement it?

Ryan
  • 41
  • 2
  • It looks like you can find answer to your question here : https://msdn.microsoft.com/en-us/library/2baksw0z%28v=vs.110%29.aspx – Fabjan Dec 28 '15 at 11:40
  • 2
    You would need to implement [`IXmlSerializable`](https://msdn.microsoft.com/en-us/library/system.xml.serialization.ixmlserializable%28v=vs.110%29.aspx), for instance as is shown in [how to derive xml element name from an attribute value of a class using annotations?](https://stackoverflow.com/questions/28769495/). But it's a nuisance, so I don't recommend bothering to do it. – dbc Dec 28 '15 at 18:48
  • I don't think `Serializable` has anything to do with XML serialization. For XML, you need to mark your class as `XmlRoot`, something like that – T.S. Dec 30 '15 at 16:31

0 Answers0