gpt4 book ai didi

asp.net - 使用返回 XML 的 Recurly Rest API 时出现 WebAPI 序列化问题

转载 作者:行者123 更新时间:2023-12-04 04:44:34 24 4
gpt4 key购买 nike

我是 ASP.Net Web API 的新手。我正在尝试与基于 Recurly REST 的 API 进行交互,并且在 ReadAsAsync 调用期间出现如下错误,我相信这是我尝试序列化响应的关键。

{"Error in line 1 position 73. Expecting element 'account' from namespace 'http://schemas.datacontract.org/2004/07/RecurlyWebApi.Recurly'.. Encountered 'Element'  with name 'account', namespace ''. "}

这是我的 HttpClient 实现,为简洁起见进行了简化:
  public class RecurlyClient
{
readonly HttpClient client = new HttpClient();

public RecurlyClient()
{
var config = (RecurlySection)ConfigurationManager.GetSection("recurly");

client.BaseAddress = new Uri(string.Format("https://{0}.recurly.com/v2/", config.Subdomain));

// Add the authentication header
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic", Convert.ToBase64String(Encoding.ASCII.GetBytes(config.ApiKey)));

// Add an Accept header for XML format.
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/xml"));
}

public T Get<T>(string id)
{
var accounts = default(T);

// Make the request and get the response from the service
HttpResponseMessage response = client.GetAsync(string.Concat("accounts/", id)).Result; // Blocking call!

if (response.IsSuccessStatusCode)
{
// Parse the response body. Blocking!
accounts = response.Content.ReadAsAsync<T>().Result;
}

return accounts;
}
}

这是我的模型:
  [XmlRoot("account")]
public class Account
{
[XmlAttribute("href")]
public string Href { get; set; }

[XmlElement("account_code")]
public string AccountCode { get; set; }

[XmlElement("state")]
public AccountState State { get; set; }

[XmlElement("username")]
public string Username { get; set; }

[XmlElement("email")]
public string Email { get; set; }

[XmlElement("first_name")]
public string FirstName { get; set; }

[XmlElement("last_name")]
public string LastName { get; set; }

[XmlElement("company_name")]
public string Company { get; set; }

[XmlElement("accept_language")]
public string LanguageCode { get; set; }

[XmlElement("hosted_login_token")]
public string HostedLoginToken { get; set; }

[XmlElement("created_at")]
public DateTime CreatedDate { get; set; }

[XmlElement("address")]
public Address Address { get; set; }
}

以及来自服务的 XML 响应示例:
<account href="https://mysubdomain.recurly.com/v2/accounts/SDTEST01">
<adjustments href="https://mysubdomain.recurly.com/v2/accounts/SDTEST01/adjustments"/>
<invoices href="https://mysubdomain.recurly.com/v2/accounts/SDTEST01/invoices"/>
<subscriptions href="https://mysubdomain.recurly.com/v2/accounts/SDTEST01/subscriptions"/>
<transactions href="https://mysubdomain.recurly.com/v2/accounts/SDTEST01/transactions"/>
<account_code>SDTEST01</account_code>
<state>active</state>
<username>myusername</username>
<email>simon@example.co.uk</email>
<first_name>First name</first_name>
<last_name>Last name</last_name>
<company_name>My Company Name</company_name>
<vat_number nil="nil"></vat_number>
<address>
<address1>My Address Line 1/address1>
<address2>My Address Line 2</address2>
<city>My City</city>
<state>My State</state>
<zip>PL7 1AB</zip>
<country>GB</country>
<phone>0123456789</phone>
</address>
<accept_language nil="nil"></accept_language>
<hosted_login_token>***</hosted_login_token>
<created_at type="datetime">2013-08-22T15:58:17Z</created_at>
</account>

最佳答案

我认为问题是因为默认情况下 DataContractSerializer 用于反序列化 XML,并且默认情况下 DataContractSerializer 使用命名空间 http://schemas.datacontract.org/2004/07/Clr.Namespace 的命名空间. (在这种情况下, Clr.Namepace 是 RecurlyWebApi.Recurly。)

因为您的 XML 具有属性,所以您需要使用 XmlSerializer 而不是 DataContractSerializer,并且您准备这样做是因为您的帐户类是用 Xml* 属性修饰的。但是,您必须使用使用 XmlSerializer 的 XmlMediaTypeFormatter。您可以按照 this page 中所述,通过在全局 XMLFormatter 上设置标志来实现此目的。 :

var xml = GlobalConfiguration.Configuration.Formatters.XmlFormatter;
xml.UseXmlSerializer = true;

或者通过将 MediaTypeFormatter 作为参数提供给 ReadAsAsync 调用:
var xmlFormatter = GlobalConfiguration.Configuration.Formatters.XmlFormatter;
xmlFormatter.UseXmlSerializer = true;
accounts = response.ReadAsAsync<T>(xmlFormatter).Result

不是 100% 确定这一点,因为这并不能解释为什么错误消息中的第一个“帐户”是小写的 - DataContractSerializer 应该忽略 XmlRoot 属性。

关于asp.net - 使用返回 XML 的 Recurly Rest API 时出现 WebAPI 序列化问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18390709/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com