I'm building a asp.net MVC program that will consume a xml-based API using httpclient and deserializing it into object using XmlSerializer.
But got an error : There is an error in XML document (1, 2).
Inner exception: {"http://schemas.datacontract.org/2004/07/Service.Models'> was not expected."}
My suspicion is somehow I messed up the model class, but I'm not sure.
Thanks!
XML from API:
<ArrayDTO xmlns:i="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://schemas.datacontract.org/2004/07/RestService.Models">
<LaborDTO>
<CreateDate>2014-04-09T09:20:15.57</CreateDate>
<CreateUser>test</CreateUser>
</LaborDTO>
<LaborDTO>
<CreateDate>2014-04-09T09:20:15.57</CreateDate>
<CreateUser>test</CreateUser>
</LaborDTO>
</ArrayDTO>
Below is my C# code:
HttpClient client = new HttpClient();
....
HttpResponseMessage response = client.GetAsync(serviceString).Result;
....
XmlSerializer ds = new XmlSerializer(typeof(ArrayDTO));
var obj = ds.Deserialize(response.Content.ReadAsStreamAsync().Result);
ArrayDTO data = (ArrayDTO) obj;
foreach (var item in ArrayDTO.collection) {
....
}
this my model class:
public class LaborDTO
{
public Nullable<System.DateTime> CreateDate { get; set; }
public string CreateUser { get; set; }
}
public class ArrayDTO
{
[XmlAttribute("xmlns")]
public string schema { get; set; }
[XmlElement("LaborDTO")]
public LaborDTO[] collection { get; set; }
}