0

SOLVED with this link

I'm developing a wcf service, with rest pattern. Now it is on a IIS server.

It has got a method callable with a post, which has to receive a xml data.

I'm trying to send this xml (with other parameter), but I'm getting only deserial errors:

in System.ServiceModel.Dispatcher.OperationFormatter.DeserializeRequest(Message message, Object[] parameters)

in System.ServiceModel.Dispatcher.DemultiplexingDispatchMessageFormatter.DeserializeRequest(Message message, Object[] parameters)

in System.ServiceModel.Dispatcher.UriTemplateDispatchFormatter.DeserializeRequest(Message message, Object[] parameters)

in System.ServiceModel.Dispatcher.DispatchOperationRuntime.DeserializeInputs(MessageRpc& rpc)

in System.ServiceModel.Dispatcher.DispatchOperationRuntime.InvokeBegin(MessageRpc& rpc)

in System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage5(MessageRpc& rpc)

in System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage31(MessageRpc& rpc)

in System.ServiceModel.Dispatcher.MessageRpc.Process(Boolean isOperationContextSet)

This is the request:

retista=0019&hashedStrig=dkfjdkfjd&dati=<ArrayOfWrapClienti xmlns="http://schemas.datacontract.org/2004/07/MultipayOnline" xmlns:i="http://www.w3.org/2001/XMLSchema-instance"><WrapClienti><CODRETE>0018</CODRETE><CODICE>20685</CODICE><NOME>A.T.E.R. Azienda Territoriale</NOME><INDIRIZZO>PIAZZA POZZA</INDIRIZZO><CITTA>Verona</CITTA><CAP>37123</CAP><PROV>VR</PROV><CODICEFISCALE>00223640236</CODICEFISCALE><PIVA>223640236</PIVA><EMAIL/><ESPOSIZ_CONTABILE>937,02</ESPOSIZ_CONTABILE><STATO>FALSE</STATO></WrapClienti></ArrayOfWrapClienti>

and this is the xml I'm trying to send:

<ArrayOfWrapClienti xmlns="http://schemas.datacontract.org/2004/07/aaa" xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
<WrapClienti>
<CODRETE>1111</CODRETE>
<CODICE>111</CODICE>
<NOME>AAA</NOME>
<INDIRIZZO>PIAZZA</INDIRIZZO>
<CITTA>AAA</CITTA>
<CAP>111</CAP>
<PROV>aa</PROV>
<CODICEFISCALE>111</CODICEFISCALE>
<PIVA>223611140236</PIVA>
<EMAIL/>
<ESPOSIZ_CONTABILE>1111</ESPOSIZ_CONTABILE>
<STATO>FALSE</STATO>
</WrapClienti>
</ArrayOfWrapClienti>

If you click here, you can find out how an xml is returned by my wcf. I thought I have to send the same xml with other data, but I get the deserial error.

Why?

How can I send the xml to my wcf? In this test phase, I'm using this.

If I can give to you other useful infos, ask me and I'll write them here.

UPDATE: The text of the error message says that there are invalid data at root level, row 1, pos 1. Also if I put

<?xml version="1.0" encoding="utf-8" ?>

at the root level.

Maybe are there some missing headers?

UPDATE server side:

this is how my server wcf is configured:

    [OperationContract]
    [WebGet(UriTemplate = "cliente.xml?retista={codret}&cliente={codiceCliente}&H={hashedString}", ResponseFormat = WebMessageFormat.Xml)]
    List<WrapClienti> GetClienteXML(string codret, string codiceCliente, string hashedString);

and this is the called method:

    public GenericResponse SetClientiXML(List<WrapClienti> clienti, string retista, string hashedString) 
    {
        var api = new API();
        return api.SetClienti(clienti, retista, hashedString);        
    }

as you can I see, I don't manage any xml/json deserialzing phase, it is managed by the wcf routine. For example, if I send a json list, it converts it to a List. This is what also with xml I want to see, but I'm getting the error at the top of this question.

ANOTHER UPDATE: I have to send 3 parameters in xml, so.... this is my last attempt, I think it is more correct than previouses. Now the server gives null reference error:

<?xml version="1.0" encoding="utf-8" ?>
<SetClientiXML
  xmlns="http://tempuri.org/">
  <XMLRequest>
    <ArrayOfWrapClienti
      xmlns="http://schemas.datacontract.org/2004/07/aaa"
      xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
      <WrapClienti>
        <CODRETE>111</CODRETE>
        <CODICE>111</CODICE>
        <NOME>aaa</NOME>
        <INDIRIZZO>aaa</INDIRIZZO>
        <CITTA>aaa</CITTA>
        <CAP>aaa</CAP>
        <PROV>aa</PROV>
        <CODICEFISCALE>111</CODICEFISCALE>
        <PIVA>2236401111236</PIVA>
        <EMAIL/>
        <ESPOSIZ_CONTABILE>111</ESPOSIZ_CONTABILE>
        <STATO>FALSE</STATO>
      </WrapClienti>
    </ArrayOfWrapClienti>
    <RETISTA>1111</RETISTA>
    <HASHEDSTRING>oklkokokokok</HASHEDSTRING>
  </XMLRequest>
</SetClientiXML>

I have also created this class:

[DataContract]
public class XMLRequest
{
    [DataMember]
    public List<WrapClienti> XML;
    [DataMember]
    public string RETISTA;
    [DataMember]
    public string HASHEDSTRING;
    public XMLRequest() { }
    public XMLRequest(List<WrapClienti> x, string r, string h)
    {
        this.HASHEDSTRING = h;
        this.XML = x;
        this.RETISTA = r;
    }
}

and the portotype now is:

public GenericResponse SetClientiXML(XMLRequest xr)
Piero Alberto
  • 3,403
  • 5
  • 46
  • 91
  • The link you provided looks like there is a blank row on top. The xml identification line must start as first character in the file. – jdweng Jul 03 '15 at 16:44
  • @jdweng blank line? where? I don't have blank line – Piero Alberto Jul 06 '15 at 06:31
  • The where link appears to have a blank first line. Your posted code is also missing the 1st line – jdweng Jul 06 '15 at 07:00
  • @jdweng that xml is automatically generated, I return a list of object and the wcf builds the xml... what can I do? I don't write that xml – Piero Alberto Jul 06 '15 at 07:15
  • What is the full exception you get? You only posted the stack trace. – CodeCaster Jul 06 '15 at 10:22
  • @CodeCaster Exception thrown by the formatter in trying to deserialize the message: Error in deserializing body of request message for operation 'SetClientiXML'. Invalid data at the root level. Line 1, position 1. '. For more details, see the server log. Stack trace of the exception: – Piero Alberto Jul 06 '15 at 10:25
  • look the last update, please – Piero Alberto Jul 06 '15 at 15:29

2 Answers2

0

Does this work?

            string input =
                "<ArrayOfWrapClienti xmlns=\"http://schemas.datacontract.org/2004/07/MultipayOnline\" xmlns:i=\"http://www.w3.org/2001/XMLSchema-instance\">" +
                "<WrapClienti>" +
                "<CODRETE>0018</CODRETE>" +
                "<CODICE>20685</CODICE>" +
                "<NOME>A.T.E.R. Azienda Territoriale</NOME>" +
                "<INDIRIZZO>PIAZZA POZZA</INDIRIZZO>" +
                "<CITTA>Verona</CITTA>" +
                "<CAP>37123</CAP>" +
                "<PROV>VR</PROV>" +
                "<CODICEFISCALE>00223640236</CODICEFISCALE>" +
                "<PIVA>223640236</PIVA>" +
                "<EMAIL/>" +
                "<ESPOSIZ_CONTABILE>937,02</ESPOSIZ_CONTABILE>" +
                "<STATO>FALSE</STATO>" +
                "</WrapClienti>" +
                "</ArrayOfWrapClienti>";

            input = "<?xml version=\"1.0\" encoding=\"utf-8\"?>" + input;​
jdweng
  • 28,546
  • 2
  • 13
  • 18
  • XML is good. How are you de-serializing? Need to find out if captured xml is same as send xml. – jdweng Jul 06 '15 at 09:54
  • look the update... it is a wcf service where I don't manage the xml reading in the server side... it is managed by wcf itself, so I don't know where I can made mods. – Piero Alberto Jul 06 '15 at 10:01
  • maybe are there some missing headers? – Piero Alberto Jul 06 '15 at 10:01
  • Yo ucan use a sniffer like wireshark or fiddler to examine ip message – jdweng Jul 06 '15 at 10:52
  • I see these headers: HEADERS Access-Control-Allow-Origin: * Cache-Control: private Content-Length: 3046 Content-Type: text/html Date: Mon, 06 Jul 2015 11:13:55 GMT Server: Microsoft-IIS/8.0 X-Aspnet-Version: 4.0.30319 X-Powered-By: ASP.NET – Piero Alberto Jul 06 '15 at 11:14
  • Contents type is text/html. Is is wrapped with ![CDATA[ ? See webpage : http://stackoverflow.com/questions/2784183/what-does-cdata-in-xml-mean – jdweng Jul 06 '15 at 11:21
  • I've read your link, but I don't understand wht CDATA can be useful for me... why do you suggest it to me? – Piero Alberto Jul 06 '15 at 11:37
  • anyway, I tried also cdata... nothing changed, I'm getting crazy – Piero Alberto Jul 06 '15 at 12:11
  • The content type header is wrong if you are just sending the xml. Not sure if you are wrapping the xml inside html. – jdweng Jul 06 '15 at 12:30
  • Try this : request.Headers.Add("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8"); – jdweng Jul 06 '15 at 12:33
  • I'm sending the xml under the label "dati" and 2 strings named "hashedString" and "retista". Still the same sad error..... – Piero Alberto Jul 06 '15 at 12:41
  • I think I have almost got it.... look the parameters of SetClientiXML under the title "Update server side" in my question!! I have to send all data in a xml, so also "retista" and "hashedString" have to be in the input xml!! I'm trying in this way now, but I haven't still found the right syntax. Do you know how I have to write it? Do you think my idea is correct? In json test I did a unique json in input, so... I think this is the way! – Piero Alberto Jul 06 '15 at 14:31
-1

Here is code to serialize and de-serialize your xml

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Xml;
using System.Xml.Serialization;

namespace ConsoleApplication2
{
    class Program
    {
        const string FILENAME = @"c:\temp\test.xml";
        static void Main(string[] args)
        {
            SetClientiXML client = new SetClientiXML()
            {
                xmlRequest = new XMLRequest()
                {
                    arrayOfWrapClienti = new ArrayOfWrapClienti()
                    {
                        wrapClient = new List<WrapClient>() {
                            new WrapClient {
                                codrete = "0018",
                                codice = "20685",
                                nome = "A.T.E.R. Azienda Territoriale",
                                indirizzo = "PIAZZA POZZA",
                                citta = "Verona",
                                cap = "37123",
                                prov = "VR",
                                codiceFiscale = "00223640236",
                                piva = "223640236",
                                esposizContabile = "937,02",
                                stato = false
                            }
                        }
                    }
                },
                retista = "3303903",
                hashedString = "oklkokokokok"
            };

            XmlSerializer serializer = new XmlSerializer(typeof(SetClientiXML));

            StreamWriter writer = new StreamWriter(FILENAME);
            serializer.Serialize(writer, client);
            writer.Flush();
            writer.Close();
            writer.Dispose();

            XmlSerializer xs = new XmlSerializer(typeof(SetClientiXML));
            XmlTextReader reader = new XmlTextReader(FILENAME);
            SetClientiXML newClient = (SetClientiXML)xs.Deserialize(reader);
        }

    }

    [XmlRoot("SetClientiXML")]
    public class SetClientiXML
    {
        [XmlElement("XMLRequest")]
        public XMLRequest xmlRequest { get; set; }
        [XmlElement("RETISTA")]
        public string retista { get; set; }
        [XmlElement("HASHEDSTRING")]
        public string hashedString { get; set; }
    }

    [XmlRoot("XMLRequest")]
    public class XMLRequest
    {
        [XmlElement("ArrayOfWrapClienti")]
        public ArrayOfWrapClienti arrayOfWrapClienti { get; set; }
    }

    [XmlRoot("ArrayOfWrapClienti")]
    public class ArrayOfWrapClienti
    {
        [XmlElement("WrapClient")]
        public List<WrapClient> wrapClient { get; set; }
    }

    [XmlRoot("WrapClient")]
    public class WrapClient
    {
        [XmlElement("CODRETE")]
        public string codrete { get; set; }
        [XmlElement("CODICE")]
        public string codice { get; set; }
        [XmlElement("NOME")]
        public string nome { get; set; }
        [XmlElement("INDIRIZZO")]
        public string indirizzo { get; set; }
        [XmlElement("CITTA")]
        public string citta { get; set; }
        [XmlElement("CAP")]
        public string cap { get; set; }
        [XmlElement("PROV")]
        public string prov { get; set; }
        [XmlElement("CODICEFISCALE")]
        public string codiceFiscale { get; set; }
        [XmlElement("PIVA")]
        public string piva { get; set; }
        [XmlElement("EMAIL")]
        public string email { get; set; }
        [XmlElement("ESPOSIZ_CONTABILE")]
        public string esposizContabile { get; set; }
        [XmlElement("STATO")]
        public Boolean stato { get; set; }

    }
}
​
jdweng
  • 28,546
  • 2
  • 13
  • 18
  • Do you have ever worked with wcf rest? It is managed by the wcf itself... the issue for me is find the right way to build the xml, but it is managed... it is the same for json... the class is the same ! :) – Piero Alberto Jul 07 '15 at 06:43
  • look this... my new question with the updated problem.. http://stackoverflow.com/questions/31262596/xmlwcf-post-request-some-parameters-are-read-other-not I hope it is clear – Piero Alberto Jul 07 '15 at 07:53
  • your class is xml specific, while I want to use a class valid both for xml and json – Piero Alberto Jul 07 '15 at 09:48