繁体   English   中英

XML对象的反序列化返回空对象

[英]Deserialization of XML object returns empty object

我正在尝试在C#中实现Ally Accounts API的一部分。 我遇到了特定端点的问题。

我想获取特定帐户的余额:

  [XmlRoot("accountbalance")]
  public class SummaryAccountBalance : Balance
  {
    [XmlElement("buyingpower")]
    public BuyingPower BuyingPower { get; set; }

    [XmlElement("fedcall")]
    public decimal FedCall { get; set; }

    [XmlElement("housecall")]
    public decimal HouseCall { get; set; }

    [XmlElement("money")]
    public AccountMoney Money { get; set; }

    [XmlElement("securities")]
    public Securities Securities { get; set; }
  }

其中Balance是一个简单的抽象类:

  public abstract class Balance
  {
    [XmlElement("account")]
    public int Account { get; set; }

    [XmlElement("accountvalue")]
    public decimal AccountValue { get; set; }
  }

对于其他端点,这很好用,例如,在端点中,我可以使用此结构来获取汇总帐户余额信息。

但是/accounts/xxxxxxx/balances.xml不想为我工作。 给定此文件:

<?xml version="1.0" encoding="UTF-8"?>
<response id="77cf30da:12df25c7074:-7ea6">
  <accountbalance>
    <account>12345678</account>
    <accountvalue>67119.41</accountvalue>
    <buyingpower>
      <cashavailableforwithdrawal>66177.48000000001</cashavailableforwithdrawal>
      <daytrading>264709.84</daytrading>
      <equitypercentage>100</equitypercentage>
      <options>66177.48000000001</options>
      <soddaytrading>264709.84</soddaytrading>
      <sodoptions>66177.48000000001</sodoptions>
      <sodstock>132354.96000000002</sodstock>
      <stock>132354.96000000002</stock>
    </buyingpower>
    <fedcall>0.0</fedcall>
    <housecall>0.0</housecall>
    <money>
      <accruedinterest>0.0</accruedinterest>
      <cash>66134.67</cash>
      <cashavailable>0.0</cashavailable>
      <marginbalance>0.0</marginbalance>
      <mmf>0.02</mmf>
      <total>66134.69</total>
      <uncleareddeposits>0.0</uncleareddeposits>
      <unsettledfunds>0.0</unsettledfunds>
      <yield>0.0</yield>
    </money>
    <securities>
      <longoptions>0.0</longoptions>
      <longstocks>57.39</longstocks>
      <options>0.0</options>
      <shortoptions>0.0</shortoptions>
      <shortstocks>0.0</shortstocks>
      <stocks>57.39</stocks>
      <total>984.72</total>
    </securities>
  </accountbalance>
</response>

以下代码检测到它确实是SummaryAccountBalance但未能填充字段:

 var serializer = new XmlSerializer(typeof(SummaryAccountBalance), new XmlRootAttribute("response"));
  return (SummaryAccountBalance)serializer.Deserialize(summaryAccountBalance);

其中summaryAccountBalance是以上文件的字节流版本。

我不确定在这里哪里出错了,我正在拔头发试图解决它。 即使逐行遍历代码,在执行反序列化步骤之前,似乎也没有任何问题。 更重要的是,此精确对象在其他端点中使用,并且这些端点实现对反序列化没有问题。

我需要做些什么?

我需要做些什么?

您缺少的是<response> 不是您的SummaryAccountBalance 这是嵌套的<accountbalance>元素。

您需要再声明一个类来描述整个XML:

[XmlRoot("response")]
public class SummaryAccountBalanceResponse
{
    [XmlAttribute("id")]
    public string Id { get; set; }

    [XmlElement("accountbalance")]
    public SummaryAccountBalance Balance { get; set; }
}

并反序列化为此类:

var serializer = new XmlSerializer(typeof(SummaryAccountBalanceResponse));
return ((SummaryAccountBalanceResponse)serializer.Deserialize(summaryAccountBalance)).Balance;

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM