Click here to Skip to main content
16,013,207 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
Please refer the below code. I am trying to write the Customers[] to the MSMQ and trying to read the same.

But it is failing with the error message
"Target type array is missing. The target type array must be set in order to deserialize the XML-formatted message."

please help!

using System;
using System.Collections.Generic;
using System.Text;
using System.Messaging;
using System.Reflection;
using System.Collections;
using System.Runtime.Serialization;
using System.Xml.Serialization;

namespace MSMQ
{
    public class Program
    {
        #region properties

        static Type[] typeHandler = new Type[2];
        static string path = @".\Private$\Myqueue";
        
        #endregion

        static void Main(string[] args)
        {
            System.Messaging.XmlMessageFormatter xmlFormatter = new XmlMessageFormatter();
            MessageQueue queue = new MessageQueue();
            Message message = new Message();
            Customer[] customers = null;
            MessageQueue recieverQueue = null;

            typeHandler[0] = typeof(Program.Customers);
            //typeHandler[1] = typeof(Program.Customer);
            typeHandler[1] = typeof(string);

            queue.Path = path;
            
            if (MessageQueue.Exists(path))
            {
                MessageQueue.Delete(path);
            }

            customers = Customers.Get();

            //preparing message body and formatting
            message.Body = customers;
            message.Formatter = new XmlMessageFormatter();
            xmlFormatter.TargetTypes = typeHandler;

            //xmlFormatter.Write(message, customers);

            //create queue
            queue = MessageQueue.Create(path);

            //sending message to queue
            queue.Formatter = new XmlMessageFormatter(new Type[] { typeof(Customers) });
            queue.Send(customers);



            //****

            recieverQueue = new MessageQueue(path);
            recieverQueue.Formatter = new XmlMessageFormatter(new Type[] { typeof(Customers) });

            //recieverQueue.Formatter = new XmlMessageFormatter(typeHandler);

            Message recievedMessage = recieverQueue.Receive();
            recievedMessage.Formatter = new XmlMessageFormatter();
            xmlFormatter.TargetTypes = typeHandler;
            
            Customers myCustomers = (Customers)recievedMessage.Body;

            foreach (Customer customer in myCustomers)
            {
                Console.WriteLine("Dear Customer, \n {0} {1} \n\n welcome to the MSMQ world your zipcode is {2}", customer.FirstName, customer.LastName, customer.ZipCode);
            }
            Console.ReadLine();
        }

        [Serializable]
        
        [XmlRoot(DataType = "string", ElementName = "Customer")]
        public class Customer
        {
            public Customer()
            {

            }


            public Customer(int iD,string fn, string ln, int zc)
            {
                this.ID = iD;
                this.FirstName = fn;
                this.LastName = ln;
                this.ZipCode = zc;
            }

            int id;
            string firstname;
            string lastname;
            int zipcode;

            [XmlElement(DataType = "int", ElementName = "CustomerID")]
            public int ID
            {
                get { return id; }
                set { id = value; }
            }


            [XmlElement(DataType="string", ElementName="CustomerFirstName")]
            public string FirstName
            {
                get { return firstname;}
                set { firstname = value; }
            }

            [XmlElement(DataType = "string", ElementName = "CustomerLastName")]
            public string LastName
            {
                get { return lastname; }
                set { lastname = value; }
            }

            [XmlElement(DataType = "int", ElementName = "CustomerZipcode")]
            public int ZipCode
            {
                get { return zipcode; }
                set { zipcode = value; }
            }

        }

        [Serializable]
        [XmlRoot(DataType="string", ElementName="Customers")]
        public class Customers  : IEnumerable
        {

            static Customer[] customers = new Customer[2];
            public Customers()
            {
                
            }

            public static Customer[] Get()
            {
                customers = new Customer[2] { new Customer(1,"Vishal", "Singla", 55423), new Customer(2,"Ashish", "Singla", 55422) };
                return customers;
            }

            IEnumerator IEnumerable.GetEnumerator()
            {
                return (customers as IEnumerable).GetEnumerator();
            }

            public Customers Add(object obj)
            {
                return new Customers();
            }
        }
    }
}
Posted
Updated 20-Feb-11 11:47am
v2
Comments
Amund Gjersøe 7-Apr-11 6:56am    
Remember to respond when a proposed solution is given.

1 solution

Hi Your Customers-class is not serializable-friendly. And, unless you need some xml-formatting, not needed. And you set your xmlFormatter.TargetTypes to typeof(Customers), while you are sending in an array of Customer.

I would have simply replaced Customers-class with a generic list List<Customer>.

You could also try serializing in your code to test how your list is serializing:
C#
XmlSerializer s = new XmlSerializer(typeof(List<customer>));
List<customer> customerCollection = new List<customer>();
customerCollection.Add(new Customer(1,"Vishal", "Singla", 55423));
customerCollection.Add(new Customer(2,"Ashish", "Singla", 55422));
MemoryStream ms = new MemoryStream();
s.Serialize(ms, customerCollection);
ms.Position = 0;
StreamReader sr = new StreamReader(ms);
string serialized = sr.ReadToEnd();
sr.Close();
ms.Close(); </customer></customer></customer>


Results in:
XML
<arrayofcustomer xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <customer>
    <customerid>1</customerid>
    <customerfirstname>Vishal</customerfirstname>
    <customerlastname>Singla</customerlastname>
    <customerzipcode>55423</customerzipcode>
  </customer>
  <customer>
    <customerid>2</customerid>
    <customerfirstname>Ashish</customerfirstname>
    <customerlastname>Singla</customerlastname>
    <customerzipcode>55422</customerzipcode>
  </customer>
</arrayofcustomer>


As you see, this serializes using XmlSerializer and also most probably using XmlMessageFormatter.
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900