1
我有一類叫做Customer
XML命名空間的Web API響應缺少
[Serializable]
[DataContract]
[XmlRoot(Namespace = "http://noatariff.com")]
public class Customer
{
public Customer()
{
DateTime now = DateTime.Now;
string datePart = now.ToString("yyyy-MM-dd");
string timePart = now.ToString("HH:mm:ss.fffzzz");
this.TimeReceived = String.Format("{0}T{1}", datePart, timePart);
}
[DataMember]
[XmlElement(Namespace = "http://noatariff.com")]
public string TimeReceived { get; set; }
}
的Web API代碼
[HttpGet]
[Route("ping")]
public HttpResponseMessage GetCustomerTime()
{
Customer cust = new Customer();
HttpResponseMessage resp = Request.CreateResponse<Customer>(HttpStatusCode.OK, cust, new XmlMediaTypeFormatter(), "application/xml");
return resp;
}
當我進入我的方法,我得到的迴應如下:
<Customer xmlns:i="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://schemas.datacontract.org/2004/07/PMPI_InterconnectService.Controllers">
<TimeReceived>2016-09-07T15:35:50.658-05:00</TimeReceived>
<head/>
</Customer>
我想獲得命名空間信息作爲迴應。
我缺少什麼?
<mcn:Customer xmlns:mcn="http://noatariff.com">
<mcn:TimeReceived>2016-09-07T15:46:46.845-05:00</mcn:TimeReceived>
</mcn:Customer>
https://stackoverflow.com/questions/17327677/xml-namespaces-in-asp-net-web-api – CodeCaster
'mcn'命名空間的迴應在哪裏?你不能添加一些不存在的東西。看起來您正在添加自己的名稱空間,因此您必須處理原始響應並進行修改以滿足您的要求。 – jdweng
[XML序列化和命名空間前綴]的可能重複(https://stackoverflow.com/q/2339782/1255289) – miken32