2017-02-27 56 views
5

所以,我使用CookComputings XMLRPC庫,能夠去跟InfusionSoft(它是一個在線CRM這是相當流行的)。主要方法是:轉換CookComputing XMLRpcStruct(IEnumerable的<Object>)和實際的C#類

[XmlRpcMethod("DataService.query")] 
IEnumerable<object> QuerySubscriptionStatus(string apiKey, 
     string table, int limit, int page, 
     IDictionary queryData, string[] selectedFields); 

我需要使用一個IEnumerable<object>可悲的是,作爲InfusionSoft XML/RPC API需要它。我希望情況並非如此,但可悲的是。

由於我使用.NET 4.5,我想我可以做一個dyanmic投:

var subStatus = proxy.QuerySubscriptionStatus(
     _key, "RecurringOrder", 500, 0, dict, sarray); 
    var result = subStatus.Cast<SubscriptionStatus>(); 

不幸的是,這是不行的,我給從C#一個很不好受錯誤:

無法轉換類型'CookComputing.XmlRpc.XmlRpcStruct爲鍵入「WBI.Model.SubscriptionStatus'.`的對象

我已經試過指定我的類作爲一個struct;嘿,我甚至試圖用XMLRpcMember()標籤指定它,但不是,它只是不會轉換。

如何與IEnumerable中的數據交互?

類/結構類型我試過

public struct SubStatus 
{ 
    public int AffiliateId; 
    public int AutoCharge; 
    public double BillingAmt; 
    public string BillingCycle; 
    public int CC1; 
    public int CC2; 
    public int ContactId; 
    public DateTime EndDate; 
    public int Frequency; 
    public int Id; 
    public DateTime LastBillDate; 
    public int LeadAffiliateId; 
    public int MaxRetry; 
    public int MerchantAccountId; 
    public DateTime NextBillDate; 
    public int NumDaysBetweenRetry; 
    public int OriginatingOrderId; 
    public DateTime PaidThruDate; 
    public int PaymentGatewayId; 
    public int ProductId; 
    public int ProgramId; 
    public string PromoCode; 
    public int Qty; 
    public string ReasonStopped; 
    public int ShippingOptionId; 
    public DateTime StartDate; 
    public string Status; 
    public int SubscriptionPlanId; 
} 

我也只是嘗試一個簡單的類與XMLRpcMember標籤:

public class SubscriptionStatus 
{ 
    [XmlRpcMember("AffiliateId")] 
    public int AffiliateId { get; set; } 
    [XmlRpcMember("AutoCharge")] 
    public int AutoCharge { get; set; } 
    [XmlRpcMember("BillingAmt")] 
    public double BillingAmt { get; set; } 
    [XmlRpcMember("BillingCycle")] 
    public string BillingCycle { get; set; } 
    [XmlRpcMember("CC1")] 
    public int CC1 { get; set; } 
    [XmlRpcMember("CC2")] 
    public int CC2 { get; set; } 
    [XmlRpcMember("ContactId")] 
    public int ContactId { get; set; } 
    [XmlRpcMember("EndDate")] 
    public DateTime EndDate { get; set; } 
    [XmlRpcMember("Frequency")] 
    public int Frequency { get; set; } 
    [XmlRpcMember("Id")] 
    public int Id { get; set; } 
    [XmlRpcMember("LastBillDate")] 
    public DateTime LastBillDate { get; set; } 
    [XmlRpcMember("LeadAffiliateId")] 
    public int LeadAffiliateId { get; set; } 
    [XmlRpcMember("MaxRetry")] 
    public int MaxRetry { get; set; } 
    [XmlRpcMember("MerchantAccountId")] 
    public int MerchantAccountId { get; set; } 
    [XmlRpcMember("NextBillDate")] 
    public DateTime NextBillDate { get; set; } 
    [XmlRpcMember("NumDaysBetweenRetry")] 
    public int NumDaysBetweenRetry { get; set; } 
    [XmlRpcMember("OriginatingOrderId")] 
    public int OriginatingOrderId { get; set; } 
    [XmlRpcMember("PaidThruDate")] 
    public DateTime PaidThruDate { get; set; } 
    [XmlRpcMember("PaymentGatewayId")] 
    public int PaymentGatewayId { get; set; } 
    [XmlRpcMember("ProductId")] 
    public int ProductId { get; set; } 
    [XmlRpcMember("ProgramId")] 
    public int ProgramId { get; set; } 
    [XmlRpcMember("PromoCode")] 
    public string PromoCode { get; set; } 
    [XmlRpcMember("Qty")] 
    public int Qty { get; set; } 
    [XmlRpcMember("ReasonStopped")] 
    public string ReasonStopped { get; set; } 
    [XmlRpcMember("ShippingOptionId")] 
    public int ShippingOptionId { get; set; } 
    [XmlRpcMember("StartDate")] 
    public DateTime StartDate { get; set; } 
    [XmlRpcMember("Status")] 
    public string Status { get; set; } 
    [XmlRpcMember("SubscriptionPlanId")] 
    public int SubscriptionPlanId { get; set; } 
} 

回答

1

於是,經過從另一個資深開發一些擴展的幫助,事實證明我們能夠做出一些改變結構:

private string[] retFlds = { "Id", "ContactId", "OriginatingOrderId", "ProgramId", "SubscriptionPlanId", "ProductId", "StartDate", "NextBillDate", "BillingCycle", "Frequency", "BillingAmt", "Status", "ReasonStopped", "AutoCharge", "CC1", "CC2", "NumDaysBetweenRetry", "MaxRetry", "MerchantAccountId", "AffiliateId", "PromoCode", "LeadAffiliateId", "Qty", "ShippingOptionId" }; 
    private string table = "RecurringOrder"; 
    private DataTable dt = new DataTable(); 
    // here's the query 
    XmlRpcStruct[] retData = proxy.Query(Auth.key, table, 1000, 0, qryData, returnFields); 
    dt = StructArrayToDT(retData); 

    public static DataTable StructArrayToDT(XmlRpcStruct[] data) 
    { 
     DataTable dt = new DataTable(); 
     if (data.Length == 0) { return dt; } 

     // do columns 
     foreach (DictionaryEntry d in data[0]) 
     { 
      dt.Columns.Add(d.Key.ToString(), typeof(object)); 
     } 

     foreach (XmlRpcStruct xmlstruct in data) 
     { 
      DataRow dr = dt.NewRow(); 
      foreach (DictionaryEntry d in xmlstruct) 
      { 
       try 
       { 
        dr[d.Key.ToString()] = d.Value; 
       } 
       catch (Exception ex) 
       { 
        // handle errors 
       } 

      } 
      dt.Rows.Add(dr); 
     } 
     return dt; 
    } 

終於可以訪問這些數據沒有任何問題了。

0

在QuerySubscriptionStatus的簽名及其返回IEnumerable的展望。同樣,如果您查看正在實現IDictionary,ICollection,IEnumerable的XmlRpcStruct(Fork of XML.Rpc.Net)的定義。因此,如果我們假設QuerySubscriptionStatus正在返回實現IEnumerable的XmlRpcStruct,那麼您正在獲得Enumeration作爲基本上物品集合(即使它包含單個物品)的響應。您正嘗試對類型Enumeration進行結構化(SubscriptionStatus),而不是集合。因此錯誤。 如果Enumeration中包含的項目類型爲SubscriptionStatus結構,那麼下面的行應該有技巧。

var resultList = subStatus.ToList<SubscriptionStatus(); 

然後循環訪問resultList以訪問來自QuerySubscriptionStatus方法的響應。

foreach(var result in resultList) 
{ 

} 

,或者如果您確信響應列表將有一個條目,那麼你也可以使用以下

var result = resultList.FirstOrDefault(); 

希望有所幫助。

+0

不幸的是,將它轉換爲列表是不可能的,因爲IEnumerable 不允許它。這是一個令人沮喪的旅程大聲笑謝謝你的建議:) '編譯器錯誤消息:CS1929:'IEnumerable '不包含'ToList'的定義和最佳擴展方法重載'Enumerable.ToList (IEnumerable )'需要類型爲'IEnumerable '的接收者' – zackrspv

相關問題