c# - 将 xml 响应映射到类?

标签 c# xml asp.net-web-api

我不知道如何将一些 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/

相关文章:

c# - 想要写我自己的 'Application Whitelisting Tool' 像 Bit9 这样的东西吗?

xml - 默认情况下 XmlParser 和 XmlSlurper 命名空间是否感知?

java - 如何制作这个菜单

javascript - 为服务器上生成的文件创建 'Save As' 按钮

httpclient - 如何在 C# 中包装 HttpClient 以实现可测试性

c# - 为什么不能使用同一个 SmtpClient 实例异步发送多封电子邮件?

c# - 如何将字节数组转换为 int 数组?

xml - 需要类型为 ="xs:dateTime"的时区

c# - GraphQL 为 .net 开发做好准备

c# 忽略 else if 语句