Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

Determining the type to deserialize to

Posted on 2014-09-19
2
108 Views
Last Modified: 2014-09-19
Hi all,

I have two applications that send messages to one another using Names Pipes.
At one end, a class is serialized into a string using standard XML Serialization
[Serializable]
public class MyClassA
{
    public String s1;
    public String s2;
    public MyClassA()
    {
        s1 = "";
        s2 = "";
    }
}

Open in new window


To perform the Serialization and Deserialization, I am using the following
public static string SerializeObject<T>(this T toSerialize)
{
    string xmlstream = String.Empty;
    using (MemoryStream memstream = new MemoryStream())
    {
        XmlSerializer xmlSerializer = new XmlSerializer(typeof(T));
        XmlTextWriter xmlWriter = new XmlTextWriter(memstream, Encoding.UTF8);
        xmlSerializer.Serialize(xmlWriter, toSerialize);
        xmlstream = UTF8ByteArrayToString(((MemoryStream)xmlWriter.BaseStream).ToArray());
    }
    return xmlstream;
}

public static T DeSerializeObject<T>(String toDeSerialize)
{
    T tempObject = default(T);
    using (MemoryStream memoryStream = new MemoryStream(StringToUTF8ByteArray(toDeSerialize)))
    {
        XmlSerializer xs = new XmlSerializer(typeof(T));
        XmlTextWriter xmlTextWriter = new XmlTextWriter(memoryStream, Encoding.UTF8);
        tempObject = (T)xs.Deserialize(memoryStream);
    }
    return tempObject;           
}

public static String UTF8ByteArrayToString(Byte[] ArrBytes) { return new UTF8Encoding().GetString(ArrBytes); }
public static Byte[] StringToUTF8ByteArray(String XmlString) { return new UTF8Encoding().GetBytes(XmlString); }

Open in new window


When I receive the message, I decode it using:
MyClassA cls = DeSerializeObject<MyClassA>(message_string);

Open in new window

and it works great.

Now, if I receive a message that is a different class, how can I determine this before attempting to Deserialize it?

Any help would be appreciated

James
0
Comment
Question by:jatkin
2 Comments
 
LVL 44

Accepted Solution

by:
AndyAinscow earned 500 total points
ID: 40332760
Assuming you are passing the XML string between the two apps via the pipe you could prepend a string with the class type.

eg.
Sending:
string s = "MyClassA;" + xmlString;
or
string s = "AnotherClassB;" + xmlString;


and receiving you strip of the first part up to the ';' and then
if(sTag == "MyClassA")
  deserialise_classA;
else if(sTag=="AnotherClassB")
  deserialise_classB
and so on
0
 
LVL 4

Author Closing Comment

by:jatkin
ID: 40332776
Perfect, thanks :-)
0

Featured Post

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…

828 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question