XML序列化:对象属性映射到2个XML元素

本文关键字:XML 2个 元素 映射 属性 序列化 对象 | 更新日期: 2023-09-27 17:51:15

我想序列化一个对象,其中一个属性映射到2个xml元素。我正在创建一个程序,它通过RESTful API与wifi帐户管理系统接口。下面是我需要序列化来创建帐户的对象:

[XmlRoot("record")]
class XmlUser 
{
    [XmlElement("login")]
    public string Username { get; set; }
    [XmlElement("password")]
    [XmlElement("password_confirmation")]
    public string Password { get; set; }
    // Other attributes...
}

在一个属性上有两个XmlElementAttributes会抛出异常,说我需要添加XmlChoiceIdentifierAttribute。我不需要反序列化对象。我应该放弃这个方法,而只使用XmlWriter吗?

XML序列化:对象属性映射到2个XML元素

你可以,

[XmlElement("password")]
public string Password { get; set; }
[XmlElement("password_confirmation")]
public string PasswordConfirmation{ get { return Password;} set; }