0

I have a MVC web api that returns List The XML returned to the client is like this:

<ArrayOfLeadChange xmlns="The-namespace">
<LeadChange>
...
<LeadChange>
<ArrayOfLeadChange>

I have copied the class LeadChange to the client and tries to Deserialize the xml to List, but i get an exception, that there is an error in the xml "Unexpected "

Is there a good way to Get or Deserialize, so that i can work with an object on the client side?

2
  • What kind of client? Javascript? .NET? Other? Commented Jan 8, 2013 at 18:04
  • The client is an ASP.NET C# application Commented Jan 8, 2013 at 18:06

2 Answers 2

2

Add a reference to System.Net.Http.Formatting - that's part of Web API, but if you don't have Web API assemblies installed on the client you can get just the client bits from Nuget - http://nuget.org/packages/Microsoft.AspNet.WebApi.Client/4.0.20710.0

PM> Install-Package Microsoft.AspNet.WebApi.Client -Version 4.0.20710.0

Then you can get from Web API and deserialize using ReadAsAsync<T> - http://msdn.microsoft.com/en-us/library/hh944541(v=vs.108).aspx

For example:

var myobjects = Request.Content.ReadAsAsync<List<LeadChange>>().Result;
Sign up to request clarification or add additional context in comments.

Comments

1

I am not sure if deserializing manually is the correct path for your use case. But, if you want a good all-around XML deserializer try this.

EDIT: This is the correct function for returning a collection.

    /// <summary>
    /// Converts an XDoc into a List<T> of entities
    /// </summary>
    /// <typeparam name="T">Any serializable object</typeparam>
    /// <param name="doc"></param>
    /// <returns></returns>
    public static List<T> DeserializeCollection<T>(XDocument doc)
    {
        if (doc == null)
            return null;

        try
        {
            XmlSerializer serializer = new XmlSerializer(typeof(List<T>));
            XmlReader reader = doc.CreateReader();
            List<T> result = (List<T>)serializer.Deserialize(reader);
            reader.Close();
            return result;
        }
        catch (Exception e)
        {
            Logging.Log(Severity.Error, "Unable to deserialize document.", e);
            return null;
        }

    }

I am not sure why you have a namespace in the <ArrayOf node. Try removing it. That should work. You can also feed your List into this function to see what the "correct"/"expected" XML should be.

    /// <summary>
    /// Converts a List<T> of entities into an XDoc.
    /// </summary>
    /// <typeparam name="T">Any serializable object</typeparam>
    /// <param name="doc"></param>
    /// <param name="paramList"></param>
    public static XDocument SerializeCollection<T>(List<T> paramList)
    {
        if (paramList == null)
            return null;

        XDocument doc = new XDocument();

        try
        {
            XmlSerializer serializer = new XmlSerializer(paramList.GetType());
            XmlWriter writer = doc.CreateWriter();
            serializer.Serialize(writer, paramList);
            writer.Close();
            return doc;
        }
        catch (Exception e)
        {
            Logging.Log(Severity.Error, "Unable to serialize list", e);
            return null;
        }

    }
}

4 Comments

This resultet in the same error. <ArrayOfLeadsChange xmlns='schemas.datacontract.org/2004/07/Name.Space'> was not expected.
It results in the same exception. I will try to remove the root node.
Sorry, I think I was unclear. You need the root node, just remove the namespace attribute.
Did you get a chance to try out the code? Any problems I can help you with?

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.