【发布时间】:2016-08-26 11:40:59
【问题描述】:
1) 第一种情况是说我的 web api 操作以 xml 格式返回数据。所以告诉我什么时候我会用 httpclient 调用那个 web api 操作,那么我怎么能将客户 xml 数据反序列化到客户端的客户类?
假设这是我的 web api 操作
[RoutePrefix("api/customer")]
public class CustomerController : ApiController
{
static readonly ICustomerRepository repository = new CustomerRepository();
[HttpGet, Route("GetAll")]
public HttpResponseMessage GetAllCustomers()
{
//return new Response(true, "SUCCESS", repository.GetAll());
return Request.CreateResponse(HttpStatusCode.OK, new Response(true, "SUCCESS", repository.GetAll()));
}
[HttpGet, Route("GetByID/{customerID}")]
public Response GetCustomer(string customerID)
{
Customer customer = repository.Get(customerID);
if (customer == null)
{
throw new HttpResponseException(HttpStatusCode.NotFound);
}
return new Response(true, "SUCCESS", customer);
//return Request.CreateResponse(HttpStatusCode.OK, response);
}
[HttpGet, Route("GetByCountryName/{country}")]
public IEnumerable<Customer> GetCustomersByCountry(string country)
{
return repository.GetAll().Where(
c => string.Equals(c.Country, country, StringComparison.OrdinalIgnoreCase));
}
public HttpResponseMessage PostCustomer(Customer customer)
{
customer = repository.Add(customer);
var response = Request.CreateResponse<Customer>(HttpStatusCode.Created, customer);
string uri = Url.Link("DefaultApi", new { customerID = customer.CustomerID });
response.Headers.Location = new Uri(uri);
return response;
}
public void PutProduct(string customerID, Customer customer)
{
customer.CustomerID = customerID;
if (!repository.Update(customer))
{
throw new HttpResponseException(HttpStatusCode.NotFound);
}
}
public void DeleteProduct(string customerID)
{
Customer customer = repository.Get(customerID);
if (customer == null)
{
throw new HttpResponseException(HttpStatusCode.NotFound);
}
repository.Remove(customerID);
}
}
public class Response
{
bool IsSuccess = false;
string Message;
object ResponseData;
public Response(bool status, string message, object data)
{
IsSuccess = status;
Message = message;
ResponseData = data;
}
}
public class Customer
{
public string CustomerID { get; set; }
public string CompanyName { get; set; }
public string ContactName { get; set; }
public string ContactTitle { get; set; }
public string Address { get; set; }
public string Region { get; set; }
public string PostalCode { get; set; }
public string City { get; set; }
public string Country { get; set; }
public string Phone { get; set; }
public string Fax { get; set; }
}
现在这样调用
var baseAddress = "http://localhost:8010/api/customer/GetAll";
using (var client = new HttpClient())
{
using (var response = await client.GetAsync(baseAddress))
{
if (response.IsSuccessStatusCode)
{
}
else
{
Console.WriteLine("{0} ({1})", (int)response.StatusCode, response.ReasonPhrase);
}
}
}
当response.IsSuccessStatusCode 为真时,请告诉我如何读取响应并从响应类中提取客户数据。
只需给我HttpClient 的示例代码,它会告诉我如何deserialize 响应和客户类。请包括我可以使用的示例代码。
只需给我示例客户端代码,它会告诉我如何保存响应中的 xml 并将其反序列化为客户类。谢谢
【问题讨论】:
标签: c# xml serialization asp.net-web-api