sensei
sensei

Reputation: 7592

XMLSerializer - how do you deserialize

How do you deserialize this:

<c:replyMessage
xmlns:c="urn:schemas-cybersource-com:transaction-data-1.118">
     <c:merchantReferenceCode>cmAction12345</c:merchantReferenceCode>
     <c:requestID>3473543574375349573452</c:requestID>
     <c:caseManagementActionReply> 
         <c:reasonCode>100</c:reasonCode>
     </c:caseManagementActionReply> 
</c:replyMessage>

I get this error in fiddler response:

<replyMessage xmlns='urn:schemas-cybersource-com:transaction-data-1.118'> was not expected.  

I used binder that accepts xml on mvc action:

public class XmlModelBinderProvider : IModelBinderProvider
{
    public IModelBinder GetBinder(Type modelType)
    {
        var contentType = System.Web.HttpContext.Current.Request.ContentType;

        if (string.Compare(contentType, @"text/xml",
            StringComparison.OrdinalIgnoreCase) != 0)
        {
            return null;
        }

        return new XmlModelBinder();
    }
}

public class XmlModelBinder : IModelBinder
{
    public object BindModel(
        ControllerContext controllerContext,
        ModelBindingContext bindingContext)
    {
        var modelType = bindingContext.ModelType;
        var serializer = new XmlSerializer(modelType);

        var inputStream = controllerContext.HttpContext.Request.InputStream;

        return serializer.Deserialize(inputStream);
    }
}

Models:

[XmlRoot("c:caseManagementActionReply")] 
public class CCaseManagementActionReply
{
    public string ReasonCode { get; set; }
}

[XmlRoot("c:replyMessage")] 
public class CReplyMessage
{
    [XmlElement(ElementName = "c:merchantReferenceCode")]
    public string MerchantReferenceCode { get; set; }

    [XmlElement(ElementName = "c:requestID")]
    public string RequestID { get; set; }

    [XmlElement(ElementName = "c:caseManagementActionReply")]
    public CCaseManagementActionReply CaseManagementActionReply { get; set; }
}

Upvotes: 1

Views: 177

Answers (1)

Volkan Paksoy
Volkan Paksoy

Reputation: 6977

You're using the same element name for different properties. Just a copy/paste error I think:

[XmlElement(ElementName = "c:merchantReferenceCode")]
public string MerchantReferenceCode { get; set; }

[XmlElement(ElementName = "c:merchantReferenceCode")]
public string Decision { get; set; }

UPDATE:

You should specify the namespace in the XmlRoot attribute. I tested the following class and it successfully serialized the sample XML:

[XmlRoot("replyMessage", Namespace= "urn:schemas-cybersource-com:transaction-data-1.118")]
public class CReplyMessage
{
    [XmlElement(ElementName = "merchantReferenceCode")]
    public string MerchantReferenceCode { get; set; }

    [XmlElement(ElementName = "requestID")]
    public string RequestID { get; set; }

    [XmlElement(ElementName = "caseManagementActionReply")]
    public CCaseManagementActionReply CaseManagementActionReply { get; set; }
}

public class CCaseManagementActionReply
{
    [XmlElement(ElementName = "reasonCode")]
    public string ReasonCode { get; set; }
}

Upvotes: 2

Related Questions