Change XmlElement name for XML serialisation

后端 未结 2 505
太阳男子
太阳男子 2021-01-01 19:23

We have the followig code:

[Serializable]
public class Class1
{
    [XmlElement(\"description\")]
    public string Description { get; set; }
}
class Program         


        
2条回答
  •  清酒与你
    2021-01-01 19:56

    Use an XmlTypeAttribute on the class as well:

    [XmlType(TypeName="ElementName")]
    [Serializable]
    public class Class1 { ...
    

    EDIT: Updated from XmlRootAttribute to XmlTypeAttribute. The former works where the type being passed to the serialiser is the attributed type (Class1 here), but not when there is a wrapping type (List here). That XmlType works is not clear from the documentation (my emphasis):

    Controls the XML schema that is generated when the attribute target is serialized by the XmlSerializer.

    Credit to Bala R's answer.

提交回复
热议问题