我有一个名为Customer
[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>
答案 0 :(得分:0)
试试这个:
[DataContract(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]
public string TimeReceived { get; set; }
}
如果您使用DataContract
作为您的serilizer,则不需要任何其他内容。