C # and Serialization: is it possible for an ElementName from XmlArrayItem to be determined by the value of an object member?

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 as:

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

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

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

that is , I expected that ElementNameof XmlArrayItemis the value of the member variable, not the name of the variable . How can i do this?

If I need to inherit ISerializable, can you tell me how to implement it?

+4
source share

Source: https://habr.com/ru/post/1621951/


All Articles