我不知道如何将一些 XML 表示为 C# 类。有没有人对如何正确映射此 xml 有任何建议?下面是我的尝试:
<authenticationResponse>
<Accounts>
<AccountId>1</AccountId>
<AccountId>5</AccountId>
</Accounts>
</authenticationResponse>
public class authenticationResponse
{
[XmlElement("Accounts")]
[DataMember]
public List<Account> Accounts { get; set; }
}
public class Account
{
public long id { get; set; }
}
最佳答案
Visual Studio 2012 有一个很酷的功能,称为“将 XML 作为类粘贴”(在“编辑”>“选择性粘贴”下)。您只需将 XML 复制到剪贴板,此“将 XML 作为类粘贴”功能将为您生成并粘贴此 authenticationResponse
类:
/// <remarks/>
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
[System.Xml.Serialization.XmlRootAttribute(Namespace = "", IsNullable = false)]
public partial class authenticationResponse
{
private byte[] accountsField;
/// <remarks/>
[System.Xml.Serialization.XmlArrayAttribute()]
[System.Xml.Serialization.XmlArrayItemAttribute("AccountId", IsNullable = false)]
public byte[] Accounts
{
get
{
return this.accountsField;
}
set
{
this.accountsField = value;
}
}
}
关于c# - 将 xml 响应映射到类?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14699242/