2017-06-05 45 views
1

我有一类叫做CustomerXML命名空间的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> 
+0

https://stackoverflow.com/questions/17327677/xml-namespaces-in-asp-net-web-api – CodeCaster

+0

'mcn'命名空间的回应在哪里?你不能添加一些不存在的东西。看起来您正在添加自己的名称空间,因此您必须处理原始响应并进行修改以满足您的要求。 – jdweng

+0

[XML序列化和命名空间前缀]的可能重复(https://stackoverflow.com/q/2339782/1255289) – miken32

回答

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你不需要任何别人的事情。