C# web api响应中缺少XML命名空间
我有一个叫做C# web api响应中缺少XML命名空间,c#,xml,asp.net-web-api,C#,Xml,Asp.net Web Api,我有一个叫做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 t
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;
}
[HttpGet]
[路线(“ping”)]
公共HttpResponseMessage GetCustomerTime()
{
客户客户=新客户();
HttpResponseMessage resp=Request.CreateResponse(HttpStatusCode.OK,cust,新的XmlMediaTypeFormatter(),“应用程序/xml”);
返回响应;
}
当我访问我的方法时,我得到如下响应:
<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>
2016-09-07T15:35:50.658-05:00
我想得到响应中的名称空间信息
我错过了什么
<mcn:Customer xmlns:mcn="http://noatariff.com">
<mcn:TimeReceived>2016-09-07T15:46:46.845-05:00</mcn:TimeReceived>
</mcn:Customer>
2016-09-07T15:46:46.845-05:00
试试这个:
[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
作为序列器,则不需要其他任何东西。只需尝试以下方法:
[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
作为序列化程序,则不需要任何其他东西。响应的“mcn”命名空间在哪里?您不能添加不存在的内容。看起来您正在添加自己的命名空间,因此必须处理原始响应并进行修改以满足您的要求。可能的重复项看起来您实际使用的是DataContractSerializer
。您是否尝试过通过[DataContract(namespace=”在契约中简单地设置名称空间http://noatariff.com“”]
如图所示?响应的“mcn”命名空间在哪里?您不能添加不存在的内容。看起来您正在添加自己的命名空间,因此必须处理原始响应并进行修改以满足您的要求。可能的重复项看起来您实际使用的是DataContractSerializer
。您是否尝试过通过[DataContract(namespace=”在契约中简单地设置名称空间http://noatariff.com”)
如图所示?