1

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; }
}

1 Answer 1

3

All you need is using the Xml namespace

[XmlRoot(Namespace = "http://schemas.datacontract.org/2004/07/RestService.Models")]
public class ArrayDTO
{
    [XmlElement("LaborDTO")]
    public LaborDTO[] collection { get; set; }
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.