我正在尝试在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; }
}
对于其他端点,此工作正常,我可以使用此结构获取汇总帐户余额信息,例如在this端点。
但是/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
是上述文件的字节流版本。
我不确定我在哪里出错,而且我想把头发拉出来试图解决它。即使逐行完成代码,在这个反序列化步骤之前,似乎根本没有任何问题。更重要的是,这个确切的对象在其他端点中使用,并且那些端点实现没有问题反序列化它。
我缺少什么让这项工作?
答案 0 :(得分:0)
我缺少什么让这项工作?
您遗失的是<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;