Wednesday, August 27, 2014

XML Deserialize Throw "xml was not expected."

Today i get error when deserialize xml string from my partner

XML

          string xmlRaw = "";
            xmlRaw += "<?xml version=\"1.0\"?>";
            xmlRaw += "<PartnerResponse xmlns=\"http://example.com\">";
            xmlRaw += "<ResultCode>00000</ResultCode>";
            xmlRaw += "<Description>Success</Description>";
            xmlRaw += "</PartnerResponse>";


Error 

{"<PartnerResponse xmlns='http://example.com'> was not expected."}




Problem
I'm not define any default name space in my class

   /// <summary>
    /// Response - Generic Response
    /// </summary>
    public class PartnerResponse
    {
        [XmlElement]
        public string ResultCode { get; set; }
        [XmlElement]
        public string Description { get; set; }
    }


Solution

Either define namespace or ask partner remove xmls from xml string.