简体   繁体   中英

How to deserialize the following XML response in C#

I received the following response from a java web service. When try to deserialize it using DataContractSerializer, I received the following error

<ns:redeemVoucherResponse xmlns:ns="http://vouchers.example.com">
    <ns:return xmlns:ax236="http://util.vouchers.example.com/xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="ax236:RedeemStatus">
        <ax236:status>fail</ax236:status>
        <ax236:statusCode>4</ax236:statusCode>
        <ax236:statusMessage>Error message</ax236:statusMessage>
        <ax236:redeemData xsi:nil="true" />
    </ns:return>
</ns:redeemVoucherResponse>


Error in line 1 position 65. Expecting element VoucherService.redeemVoucherResponse' from namespace 'http://schemas.datacontract.org/2004/07/Service.Internal.VoucherServices'.. Encountered 'Element'  with name 'redeemVoucherResponse', namespace 'http://vouchers.example.com'.

Classes use for the deserialization:

public class redeemVoucherResponse {
        [DataMember]
        public RedeemStatus @return { get; set; }
    }

    public class RedeemStatus {
        [DataMember]
        public string status { get; set; }

        [DataMember]
        public byte statusCode { get; set; }

        [DataMember]
        public string statusMessage { get; set; }

        [DataMember]
        public object redeemData { get; set; }
    }

Code use for the deserialization:

HttpWebResponse objWebResponse = HttpWebResponse)objWebRequest.GetResponse();
Stream objResponseStream = objWebResponse.GetResponseStream(); 

Type objType = typeof(redeemVoucherResponse);
DataContractSerializer objXmlSerializer = new DataContractSerializer(objType);

redeemVoucherResponse objMessage = (redeemVoucherResponse)objXmlSerializer.ReadObject(objResponseStream);

Try this ... not tested though.

[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true, Namespace = "http://vouchers.example.com")]
[System.Xml.Serialization.XmlRootAttribute(Namespace = "http://vouchers.example.com", IsNullable = false)]
public class redeemVoucherResponse
{
    public redeemVoucherResponseReturn @return { get; set; }
}

[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true, Namespace = "http://vouchers.example.com")]
public class redeemVoucherResponseReturn
{
    [System.Xml.Serialization.XmlElementAttribute(Namespace = "http://util.vouchers.example.com/xsd")]
    public string status { get; set; }

    [System.Xml.Serialization.XmlElementAttribute(Namespace = "http://util.vouchers.example.com/xsd")]
    public byte statusCode { get; set; }

    [System.Xml.Serialization.XmlElementAttribute(Namespace = "http://util.vouchers.example.com/xsd")]
    public string statusMessage { get; set; }

    [System.Xml.Serialization.XmlElementAttribute(Namespace = "http://util.vouchers.example.com/xsd", IsNullable = true)]
    public object redeemData { get; set; }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM