Question

For reasons I wont' go into here, I can't use the object type as a parameter into my webapi controller. Therefore, I need to figure out a way to deserialize the xml object into my c# object using XmlDocument, or something similar.

Here's what I have so far:

    public void Post(HttpRequestMessage request)
    {
        var xmlDoc = new XmlDocument();
        xmlDoc.Load(request.Content.ReadAsStreamAsync().Result);
        using (XmlReader xmlReader = new XmlNodeReader(xmlDoc))
        {
            Object obj = new XmlSerializer(typeof(myObject)).Deserialize(xmlReader);
            myObject scp = (myObject)obj;
        } 
    }

unfortunately that's throwing errors. Can anyone provide some suggestion as to how I can deserialize my xml into my object?

tia

Edit: here's the xml I'm trying to deserialize:

<Student>
<studentid>1234</studentid>
<command>post</command>
<posttype>charge</posttype>
<transaction_description>This is a test post to the web api</transaction_description>
<payment_type>CC</payment_type>
<term_code>2013SPRING</term_code>
<amount>432.75</amount>
</Student>

and here's the error I'm getting:

System.InvalidOperationException: was not expected. Generated: Wed, 19 Mar 2014 20:18:58 GMT

System.InvalidOperationException: There is an error in XML document (1, 2). ---> System.InvalidOperationException: was not expected. at Microsoft.Xml.Serialization.GeneratedAssembly.XmlSerializationReaderStudentChargePost.Read3_StudentChargePost() --- End of inner exception stack trace --- at System.Xml.Serialization.XmlSerializer.Deserialize(XmlReader xmlReader, String encodingStyle, XmlDeserializationEvents events)
at System.Xml.Serialization.XmlSerializer.Deserialize(Stream stream)
at CashNetSSO.Controllers.API.StudentInformationPostController.Post(HttpRequestMessage request) in C:\Projects\CashNetSSO\Development\CashNetSSO\CashNetSSO\Controllers\API\StudentInformationPostController.cs:line 23 at lambda_method(Closure , Object , Object[] ) at System.Web.Http.Controllers.ReflectedHttpActionDescriptor.ActionExecutor.<>c_DisplayClassf.b_9(Object instance, Object[] methodParameters) at System.Web.Http.Controllers.ReflectedHttpActionDescriptor.ActionExecutor.Execute(Object instance, Object[] arguments) at System.Web.Http.Controllers.ReflectedHttpActionDescriptor.<>c_DisplayClass5.b_4() at System.Threading.Tasks.TaskHelpers.RunSynchronously[TResult](Func`1 func, CancellationToken cancellationToken)

Was it helpful?

Solution

If you are already reading the content as a stream you can do the following:

    myObject scp = null;
    XmlSerializer serializer = new XmlSerializer(typeof(myObject);
    using (Stream stream = request.Content.ReadAsStreamAsync().Result)
    {
        scp = serializer.Deserialize(stream);
    }

Edit:

The reason you are getting the error is because the XmlSerializer is expecting an xml declaration tag. If your xml does not contain this you can define the root attribute like below:

    XmlSerializer serializer = new XmlSerializer(typeof(myObject), new XmlRootAttribute("Student"));
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top