0
我有一个像的WebAPI方法,从而带来:的WebAPI调用的XML命名空间,导致反序列化失败
[HttpPost, Route("api/student/studentstatus")]
public UpdatedStudent StudentStatus(HttpRequestMessage request)
{
string requestData = request.Content.ReadAsStringAsync().Result;
GetStudentStatus scp = null;
XmlSerializer serializer = new XmlSerializer(typeof(GetStudentStatus), new XmlRootAttribute("GetStudentStatus"));
using (Stream stream = request.Content.ReadAsStreamAsync().Result)
{
scp = (GetStudentStatus)serializer.Deserialize(stream);
}
}
这是的RequestData字符串
<GetStudentStatus xmlns:i="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://schemas.datacontract.org/2004/07/StudentStatusConsole">
<StudentID></StudentID><Batch></Batch>
</GetStudentStatus>
当StudentStatus被击中死了在这个错误的Deserialize部分:
"<GetStudentStatus xmlns='http://schemas.datacontract.org/2004/07/StudentStatusConsole'> was not expected."}
这是GetStudentStatus类:
public class GetStudentStatus
{
public string StudentID { get; set; }
public string Batch { get; set; }
}
试图与DataContract空间(namespace = “”)和添加数据成员属性的属性来注释的类。 不行。
这是调用的WebAPI的方法:
using (var client = new HttpClient())
{
GetStudentStatus studentToPost = new GetStudentStatus() { StudentID = "", Batch = "" };
client.BaseAddress = new Uri("http://localhost:53247/");
client.DefaultRequestHeaders.Accept.Clear();
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/xml"));
HttpResponseMessage response = await client.PostAsXmlAsync("api/student/studentstatus", studentToPost);
}
任何帮助不胜感激。