How can I override the serialized name of each list item in List <SomeStruct> () in C #?
I have a structure more or less like this:
[Serializable]
[XmlRoot("Customer")]
public struct TCustomer
{
string CustomerNo;
string Name;
}
I sometimes serialize this structure in XML as a single object that works fine, but sometimes I also need to serialize the List <> of this structure.
I used this to set the name of the top-level element:
[Serializable]
[XmlRoot("Customers")]
public class CustomerList : List<TCustomer> { }
XmlSerializer, however, insists on invoking each item in the TCustomer list. How can I tell the XmlSerializer to use the name Customer instead of TCustomer?
XmlRoot , TCustomer CustomerList.
, TCustomer Customer CustomerList. - ...
[Serializable]
[XmlRoot("customerList")]
public class CustomerList
{
[XmlArray("customers")]
[XmlArrayItem("customer")]
public List<TCustomer> Customers { get; set; }
}
xml, :
<customerList>
<customers>
<customer />
<customer />
<customer />
</customers>
</customerList>
, .
IXmlSerializable, :
In any case, it seems that the serializer calls TCustomerthe element type because it is the actual type List( TCustomeris ???, where ??? will be the actual type of the generic parameter type TCustomer).
I would not change the way XmlSerializerobjects are serialized without implementation IXmlSerializable(I mean, I avoid replacing text or any other after serialization!).