Click here to Skip to main content
15,888,527 members
Home / Discussions / C#
   

C#

 
AnswerRe: How to use a function pointer in C# Pin
Rolando CC30-Apr-09 6:38
professionalRolando CC30-Apr-09 6:38 
GeneralRe: How to use a function pointer in C# Pin
Member 103390730-Apr-09 22:49
Member 103390730-Apr-09 22:49 
AnswerRe: How to use a function pointer in C# Pin
MidwestLimey30-Apr-09 6:41
professionalMidwestLimey30-Apr-09 6:41 
GeneralRe: How to use a function pointer in C# Pin
Member 103390730-Apr-09 22:50
Member 103390730-Apr-09 22:50 
QuestionReuse existing MFC project in C# Pin
Katherine Williams30-Apr-09 5:00
Katherine Williams30-Apr-09 5:00 
AnswerRe: Reuse existing MFC project in C# Pin
Henry Minute30-Apr-09 5:34
Henry Minute30-Apr-09 5:34 
AnswerRe: Reuse existing MFC project in C# Pin
S. Senthil Kumar30-Apr-09 9:39
S. Senthil Kumar30-Apr-09 9:39 
QuestionDynamic XML Serialiser Pin
BASONJS30-Apr-09 4:23
BASONJS30-Apr-09 4:23 
Hi guys,

we are back. I have a small problem with xml serialization.

Problem:
I can serialize an object as the type is know from the object passed in to be serialized, but after this is passed through a webservice, deserialization is a problem, as the type of the object is not known. The objects which are being serialized are all custom objects.

Scenario:
I have a generic serializer class. The code found below:

/// <summary>
/// Method to convert a custom Object to XML string
/// </summary>
/// <param name="pObject">Object that is to be serialized to XML</param>
/// <returns>XML string</returns>
public String serializeObject(Object pObject)
{
    try
    {
        String XmlizedString = null;
        MemoryStream memoryStream = new MemoryStream();
        XmlSerializer xs = new XmlSerializer(pObject.GetType());
        XmlTextWriter xmlTextWriter = new XmlTextWriter(memoryStream, Encoding.UTF8);
        xs.Serialize(xmlTextWriter, pObject);
        memoryStream = (MemoryStream)xmlTextWriter.BaseStream;
        XmlizedString = UTF8ByteArrayToString(memoryStream.ToArray());
        return XmlizedString;
    }
    catch (Exception e)
    {
        System.Console.WriteLine(e);
        return null;
    }
}

/// <summary>
/// Method to reconstruct an Object from XML string
/// </summary>
/// <param name="pXmlizedString"></param>
/// <returns></returns>
public Object deserializeObject(String pXmlizedString)
{
    XmlSerializer xs = new XmlSerializer(typeof(Object));
    MemoryStream memoryStream = new MemoryStream(StringToUTF8ByteArray(pXmlizedString));
    XmlTextReader xmlTextReader = new XmlTextReader(memoryStream);
    return xs.Deserialize(xmlTextReader);
}


When I am serializing the object, this works fine, as I can create a serializer with the object type from the object:

XmlSerializer xs = new XmlSerializer(pObject.GetType());


But when I come to deserialize the object from a string, I see two options which both dont work.

Either:
XmlSerializer xs = new XmlSerializer(typeof(Object));

Which gives me a "<custom class=""> not expected error" as the serializer obviously doesnt know about my custom class.

Or:
//strType = String equal to "Namespace + Class Name". Ex: MyPackage.MyCustomClass
object newObject = Activator.CreateInstance(Type.GetType(strType));
Type type = Type.GetType(strType);
XmlSerializer xs2 = new XmlSerializer(typeof(Object));

Which again does not work as the serializer does not know of MyPackage.MyCustomClass

any thoughts on this will be highly appreciated.
Thanks
QuestionBlog site using asp.net 2.0/C# Pin
Member 383476030-Apr-09 2:44
Member 383476030-Apr-09 2:44 
AnswerRe: Blog site using asp.net 2.0/C# Pin
ATCsharp30-Apr-09 2:46
ATCsharp30-Apr-09 2:46 
AnswerRe: Blog site using asp.net 2.0/C# Pin
dan!sh 30-Apr-09 2:51
professional dan!sh 30-Apr-09 2:51 
AnswerRe: Blog site using asp.net 2.0/C# Pin
saanj30-Apr-09 3:07
saanj30-Apr-09 3:07 
Question.NET Remoting - Auto Start Service Object Pin
ATCsharp30-Apr-09 2:31
ATCsharp30-Apr-09 2:31 
AnswerRe: .NET Remoting - Auto Start Service Object Pin
ATCsharp30-Apr-09 3:45
ATCsharp30-Apr-09 3:45 
QuestionMp3 Player Pin
sagiklan30-Apr-09 2:17
sagiklan30-Apr-09 2:17 
AnswerRe: Mp3 Player Pin
DaveyM6930-Apr-09 2:20
professionalDaveyM6930-Apr-09 2:20 
AnswerRe: Mp3 Player Pin
Alaa' Al Atrash30-Apr-09 2:35
Alaa' Al Atrash30-Apr-09 2:35 
QuestionUnboxing an unknown type Pin
mtherien30-Apr-09 1:17
mtherien30-Apr-09 1:17 
AnswerRe: Unboxing an unknown type Pin
musefan30-Apr-09 1:34
musefan30-Apr-09 1:34 
GeneralRe: Unboxing an unknown type Pin
DaveyM6930-Apr-09 1:42
professionalDaveyM6930-Apr-09 1:42 
GeneralRe: Unboxing an unknown type Pin
musefan30-Apr-09 2:51
musefan30-Apr-09 2:51 
AnswerRe: Unboxing an unknown type Pin
DaveyM6930-Apr-09 1:41
professionalDaveyM6930-Apr-09 1:41 
GeneralRe: Unboxing an unknown type Pin
mtherien30-Apr-09 3:02
mtherien30-Apr-09 3:02 
AnswerRe: Unboxing an unknown type Pin
Alan N30-Apr-09 1:59
Alan N30-Apr-09 1:59 
AnswerRe: Unboxing an unknown type Pin
Gideon Engelberth30-Apr-09 2:47
Gideon Engelberth30-Apr-09 2:47 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.