Using WCF RESTful, you can find out a problem that will make you loss a lot of time. The syntax of the XML, the structure and.. the order of your fields!!! How to solve it?

Let’s you have a method (API) callable with a POST defined in this way:

[OperationContract]
    [WebInvoke(UriTemplate = "SetClienti.xml", Method = "POST", BodyStyle=WebMessageBodyStyle.Wrapped, RequestFormat = WebMessageFormat.Xml)]
    GenericResponse SetClientiXML(List<WrapClienti> dati, string retista, string hashedString);

where WrapClienti is a custom class, with a set of attributes. Look also at the public constructor without any parameters: it is very important to write it, because it allows WCF to automatically convert the XML in your class. For this goal, you have also to define the get/set method as below. And I also say that a class write in this way will be compatible also for the automatic conversion from JSON input.

public class WrapClienti
    {
        public WrapClienti() {}

        private string p_CODICE { get; set; }
        public string CODICE
        {
            get
            {
                return this.model.CODICE.ToSafeString();
            }
            set { p_CODICE = value; }
        }
        private string p_NOME { get; set; }
        public string NOME
        {
             ...
        }
        private string p_CODRETE { get; set; }
        public string CODRETE
        {
            ...
        }
        private string p_INDIRIZZO { get; set; }
        public string INDIRIZZO
        {
            ...
        }
        private string p_CITTA { get; set; }
        public string CITTA
        {
            ...
        }
        private string p_CAP { get; set; }
        public string CAP
        {
            ...
        }
        private string p_PROV { get; set; }
        public string PROV
        {
            ...
        }
        private string p_TELEFONO { get; set; }
        public string TELEFONO
        {
            ...
        }
        private string p_EMAIL { get; set; }
        public string EMAIL
        {
            ...
        }
        private string p_OSSERVAZIONI { get; set; }
        public string OSSERVAZIONI
        {
            ...
        }
        private string p_PIVA { get; set; }
        public string PIVA
        {
            ...
        }
        private string p_CODICEFISCALE { get; set; }
        public string CODICEFISCALE
        {
            ...
        }
        private string p_TOT_RICARICA { get; set; }
        public string TOT_RICARICA
        {
            ...
        }
        private string p_STATO { get; set; }
        public string STATO
        {
            ...
        }
        private string p_MAXPERIODO { get; set; }
        public string MAXPERIODO
        {
            ...
        }
        private string p_DEBITOPERIODO { get; set; }
        public string DEBITOPERIODO
        {
            ...
        }
        private string p_ESPOSIZ_CONTABILE { get; set; }
        public string ESPOSIZ_CONTABILE
        {
            ...
        }
        private string p_CODPP { get; set; }
        public string CODPP
        {
            ...
        }
        private string p_FATTURA { get; set; }
        public string FATTURA
        {
            ...
        }
        private string p_ESTRATTOCONTO { get; set; }
        public string ESTRATTOCONTO
        {
            ...
        }
    }

Well, how to write the XML to send? Look the structure to create for match the prototype of “SetClientiXML”. The parameters must have the corresponding tag. Furhter, the attributes of WrapClienti have to be… alphabetically sorted!!!! If you don’t do it, you will find only some attributes populated and the other.. null!!

So, for example, a valid XML is:

<?xml version="1.0" encoding="utf-8" ?><SetClientiXML xmlns="http://tempuri.org/">
    <dati>
        <WrapClienti xmlns="http://schemas.datacontract.org/2004/07/..." xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
            <CAP>10040</CAP>
            <CITTA>Volvera</CITTA>
            <CODICE>00001</CODICE>
            <CODICEFISCALE>123456789</CODICEFISCALE>
            <CODRETE>0001</CODRETE>
            <EMAIL/>
            <ESPOSIZ_CONTABILE>937,02</ESPOSIZ_CONTABILE>
            <INDIRIZZO>Via Roma</INDIRIZZO>
            <NOME>Mario Rossi</NOME>
            <PIVA>123456789</PIVA>
            <PROV>TO</PROV>
            <STATO>FALSE</STATO>
        </WrapClienti>
    </dati>
    <retista>0001</retista>
    <hashedString> cP5Xfy7aYPhjiT7Vddi4Mg=</hashedString>
</SetClientiXML>

Advertisements