Xml

hey guys,

i need help with a xml error, i’m trying to send a xml file to a CCV POS Engine
but i keep on getting this error (uploaded a jpg).
Exception: System.InvalidOperationException: The XML-document (2, 2) contains a error. —> System.InvalidOperationException: was not expected

if i dont add the xmlns attribute i get the same error.

        public static byte[] LoginRequest()
        {
            MemoryStream ms = new MemoryStream();
            XmlWriter xmlwrite = XmlWriter.Create(ms);

            xmlwrite.WriteProcessingInstruction("xml", "version=\"1.0\" encoding=\"utf-8\"");

            xmlwrite.WriteStartElement("ServiceRequest");
            xmlwrite.WriteAttributeString("WorkstationID", WorkstationId); 
            xmlwrite.WriteAttributeString("RequestID", "1");
            xmlwrite.WriteAttributeString("RequestType", "Login");
            xmlwrite.WriteAttributeString("xmlns", null, "http://www.nrf-arts.org/IXRetail/namespace");

            xmlwrite.WriteStartElement("POSdata");
            xmlwrite.WriteAttributeString("LanguageCode", "nl");

            xmlwrite.WriteStartElement("POSTimeStamp");
            xmlwrite.WriteString(time);
            xmlwrite.WriteEndElement();

            xmlwrite.WriteStartElement("ShiftNumber");
            xmlwrite.WriteString("0");
            xmlwrite.WriteEndElement();

            xmlwrite.WriteStartElement("PrinterStatus");
            xmlwrite.WriteString("Unavailable");
            xmlwrite.WriteEndElement();

            xmlwrite.WriteEndElement();
            xmlwrite.WriteEndElement();         // End root

            xmlwrite.Flush();
            xmlwrite.Close();

            return ms.ToArray();
        }

It looks like you’re trying to create more than one root element. Everything after the ServiceRequest element needs to be a descendant node. Can you post the xml document this generates?

EricH,

this is the xml it generates everything seems fine

<?xml version="1.0" encoding="utf-8"?> 05/19/2011 13:58:42 0 Unavailable

this is how the documentation said it should looke like

<?xml version="1.0" encoding="utf-8" ?> {PosTimeStamp} {ShiftNumber} {PrinterStatus}

regards

Can you show your stack trace?

Nevermind about the stack.

Change the following lines:


xmlwrite.Flush();
xmlwrite.Close();
return ms.ToArray();

to:


xmlwrite.Flush();
byte[] bytes = ms.ToArray();
xmlwrite.Close();
return bytes;

it doesnt seem to do the trick, the same invalidOperationException ??? .

i forgot to say that i’m sending xml over TCP/IP could it be. that the other end is mis interpetting the xmlns=“http://www.nrf-arts.org/IXRetail/namespace” Attributte.

We need to see more code.

The xml is well formed, so I’m guessing something is up with the validation against the namespace on the POS side…

This is my CLient Code

using System.Net.Sockets;
using System.Threading;
using Microsoft.SPOT;
using OPI_POS.Classes;

namespace OPI_POS.Classes
{
    class TCPClient
    {

        private const string ServerAddress = "127.0.0.1";
        private const int port = 4200;    // eft-engine 4200

        public TCPClient()
        {
            using (Socket clientSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp))
            {
                // Adressing
                IPHostEntry entry = Dns.GetHostEntry(ServerAddress);
                IPAddress ipAddress = entry.AddressList[0];
                IPEndPoint serverEndPoint = new IPEndPoint(ipAddress, port);
                Debug.Print("Connecting to CCV Pos Engine " + serverEndPoint + ".");
                clientSocket.Connect(serverEndPoint);
                Debug.Print("Connected to CCV Pos Engine.");

                //Sending
                clientSocket.Send(Request.LoginRequest());
            }  



        }
        
    }
}

My Main

using System;
using System.Threading;
using Microsoft.SPOT;
using OPI_POS.Classes;

namespace OPI_POS
{
    public class Program
    {
        public static void Main()
        {
            
            Thread mythread1 = new Thread(tcp);
            Thread mythread2 = new Thread(server);

            mythread2.Start();
            mythread1.Start();

            Thread.Sleep(Timeout.Infinite);
        }

        private static void tcp()
        {
            TCPClient tcp = new TCPClient();
        }

        private static void server()
        {
            TCPServer tcp = new TCPServer();
        }
    }
}

this is my requests class all the function keep givng the same error

using System;
using System.IO;
using System.Xml;
using System.Ext.Xml;
using Microsoft.SPOT;


namespace OPI_POS.Classes
{
    
    public static class Request
    {

        private static string WorkstationId = "EmbeddedKiosk";
        private static string time = DateTime.Now.ToString();

        public static string SendRequest()
        {
            MemoryStream ms = new MemoryStream();
            XmlWriter xmlwrite = XmlWriter.Create(ms);


            xmlwrite.WriteRaw("\r\n");

            xmlwrite.WriteProcessingInstruction("xml", "version=\"1.0\" encoding=\"utf-8\"");

            xmlwrite.WriteStartElement("CardServiceRequest");
            xmlwrite.WriteAttributeString("WorkstationID", WorkstationId);
            xmlwrite.WriteAttributeString("RequestID","1");
            xmlwrite.WriteAttributeString("RequestType","CardPayment");
            xmlwrite.WriteAttributeString("ApplicationID","{ApplicationID}");
 
            xmlwrite.WriteString("\r\n\t");
            xmlwrite.WriteStartElement("POSData");
            xmlwrite.WriteAttributeString("LanguageCode", "nl"); 
            xmlwrite.WriteAttributeString("ApprovalCode", "{ApprovalCode}");

            xmlwrite.WriteString("\r\n\t");
            xmlwrite.WriteStartElement("POSTimeStamp");
            xmlwrite.WriteString(time);
            xmlwrite.WriteEndElement();

            xmlwrite.WriteString("\r\n\t");
            xmlwrite.WriteStartElement("ShiftNumber");
            xmlwrite.WriteString("0");
            xmlwrite.WriteEndElement();

            xmlwrite.WriteString("\r\n\t");
            xmlwrite.WriteStartElement("ClerkID");
            xmlwrite.WriteString("{ClerkID}");
            xmlwrite.WriteEndElement();

            xmlwrite.WriteString("\r\n\t");
            xmlwrite.WriteStartElement("PrinterStatus");
            xmlwrite.WriteString("Unavailable");
            xmlwrite.WriteEndElement();

            xmlwrite.WriteString("\r\n\t");
            xmlwrite.WriteStartElement("VoiceReferral");
            xmlwrite.WriteString("{VoiceReferral}");
            xmlwrite.WriteEndElement();

            xmlwrite.WriteString("\r\n\t");
            xmlwrite.WriteStartElement("MerchantReference");
            xmlwrite.WriteString("{MerchantReference}");
            xmlwrite.WriteEndElement();

            xmlwrite.WriteString("\r\n\t");
            xmlwrite.WriteEndElement();

            xmlwrite.WriteString("\r\n\t");
            xmlwrite.WriteStartElement("TotalAmount");
            xmlwrite.WriteAttributeString("Currency", "EUR");
            xmlwrite.WriteString("50");
            xmlwrite.WriteEndElement();

            xmlwrite.WriteString("\r\n\t");
            xmlwrite.WriteEndElement();         // End root

            xmlwrite.Flush();
            xmlwrite.Close();
            //////// display the XML data ///////////
            byte[] byteArray = ms.ToArray();
            char[] cc = System.Text.UTF8Encoding.UTF8.GetChars(byteArray);
            string str = new string(cc);
            Debug.Print(str);
            return byteArray.ToString();
        }

        public static byte [] CardServiceRequest() 
        {
            MemoryStream ms = new MemoryStream();
            XmlWriter xmlwrite = XmlWriter.Create(ms);

            //xmlwrite.WriteRaw("\r\n\t");

            xmlwrite.WriteProcessingInstruction("xml", "version=\"1.0\" encoding=\"utf-8\"");

            xmlwrite.WriteStartElement("CardServiceRequest");
            xmlwrite.WriteAttributeString("WorkstationID", WorkstationId);
            xmlwrite.WriteAttributeString("RequestID","1");
            xmlwrite.WriteAttributeString("RequestType","CardPayment");
            xmlwrite.WriteAttributeString("xmlns",null,"http://www.nrf-arts.org/IXRetail/namespace");
 
            //xmlwrite.WriteString("\r\n\t");
            xmlwrite.WriteStartElement("POSdata");
            xmlwrite.WriteAttributeString("LanguageCode", "nl");

            //xmlwrite.WriteString("\r\n\t");
            xmlwrite.WriteStartElement("POSTimeStamp");
            xmlwrite.WriteString(time);
            xmlwrite.WriteEndElement();

            //xmlwrite.WriteString("\r\n\t");
            xmlwrite.WriteStartElement("ShiftNumber");
            xmlwrite.WriteString("0");
            xmlwrite.WriteEndElement();

            //xmlwrite.WriteString("\r\n\t");
            xmlwrite.WriteStartElement("PrinterStatus");
            xmlwrite.WriteString("Unavailable");
            xmlwrite.WriteEndElement();

            //xmlwrite.WriteString("\r\n\t");
            xmlwrite.WriteEndElement();

            //xmlwrite.WriteString("\r\n\t");
            xmlwrite.WriteStartElement("TotalAmount");
            xmlwrite.WriteAttributeString("Currency", "EUR");
            xmlwrite.WriteString("5.00");
            xmlwrite.WriteEndElement();

            //xmlwrite.WriteString("\r\n\t");
            xmlwrite.WriteEndElement();         // End root

            xmlwrite.Flush();
            xmlwrite.Close();

            
            //////// display the XML data ///////////
            byte[] byteArray = ms.ToArray();
            char[] cc = System.Text.UTF8Encoding.UTF8.GetChars(byteArray);
            string str = new string(cc); 
            Debug.Print(str);

            return ms.ToArray();
        }

        
        public static byte[] LoginRequest()
        {
            MemoryStream ms = new MemoryStream();
            XmlWriter xmlwrite = XmlWriter.Create(ms);

            xmlwrite.WriteProcessingInstruction("xml", "version=\"1.0\" encoding=\"utf-8\"");
            xmlwrite.WriteString("\r\t");
            xmlwrite.WriteStartElement("ServiceRequest");
            xmlwrite.WriteAttributeString("WorkstationID", WorkstationId); 
            xmlwrite.WriteAttributeString("RequestID", "1");
            xmlwrite.WriteAttributeString("RequestType", "Login");
            xmlwrite.WriteAttributeString("xmlns","http://www.nrf-arts.org/IXRetail/namespace");

            xmlwrite.WriteString("\r\t");
            xmlwrite.WriteStartElement("POSdata");
            xmlwrite.WriteAttributeString("LanguageCode", "nl");
            xmlwrite.WriteString("\r\t");
            xmlwrite.WriteStartElement("POSTimeStamp");
            xmlwrite.WriteString(time);
            xmlwrite.WriteEndElement();
            xmlwrite.WriteString("\r\t");
            xmlwrite.WriteStartElement("ShiftNumber");
            xmlwrite.WriteString("0");
            xmlwrite.WriteEndElement();
            xmlwrite.WriteString("\r\t");
            xmlwrite.WriteStartElement("PrinterStatus");
            xmlwrite.WriteString("Unavailable");
            xmlwrite.WriteEndElement();
            xmlwrite.WriteString("\r\t");
            xmlwrite.WriteEndElement();
            xmlwrite.WriteString("\r\t");
            xmlwrite.WriteEndElement();         // End root

            xmlwrite.Flush();
            byte[] byteArray = ms.ToArray();
            xmlwrite.Close();


            //////// display the XML data ///////////
            
            char[] cc = System.Text.UTF8Encoding.UTF8.GetChars(byteArray);
            string str = new string(cc);
            Debug.Print(str);

            return byteArray;
        }
    }
}

Hope this helps for a solution

regards

Try running your application from emulator.

yep i already did that still :frowning: :frowning:

Running out of suggestions here. :frowning:
Do you have any technical docs on POS engine?

I’d like to try without “\r\t” text nodes: they’re actually sibling nodes on infoset and I am not sure that XML rendering engine is able to manage them.

i did try it without “\r\t” text nodes already, but still getting the same error, communicating with de engine with delphi on pc works fine, but migrating it in c# micro is a bit of a struggle. :frowning:

@ Architect i dont have engine documentation only instructions for xml requests.

does regular .Net from PC works?

Is that server case-insensitive? Else you can try to use the correct casing in f.e. WorkStationID and POSData …

sorry for the late reply, but regular doen’t work either…

it just saw something very stupid of me, i was using the wrong endpoint duhh 8) ???,