[Mono-list] Problem with XML-Serialization

Blackskyliner blackskyliner at live.de
Tue Jul 14 11:56:13 EDT 2009


I serialize this class via the XML Serializer:


class XMLStringSerializer
    {
        public static string Serialize<T>(T entity)
        {
            return Serialize(entity, true);
        }

        public static string Serialize<T>(T entity, bool noNewLine)
        {
            StringBuilder outputXml = new StringBuilder();

            if (typeof(T).IsSerializable)
            {
                XmlSerializer ser = new XmlSerializer(typeof(T));
                using (TextWriter stream = new StringWriter(outputXml))
                {
                    ser.Serialize(stream, entity);
                }
                if (noNewLine)
                    return outputXml.ToString().Replace(Environment.NewLine,
"");
                else
                    return outputXml.ToString();
            }

            return null;
        }

        public static T Deserialize<T>(string xml)
        {
            T entity;

            if (typeof(T).IsSerializable)
            {
                XmlSerializer ser = new XmlSerializer(typeof(T));
                using (TextReader stream = new StringReader(xml))
                {
                    entity = (T)ser.Deserialize(stream);
                }

                return entity;
            }

            return default(T);
        }
    }





Atsushi Eno-3 wrote:
> 
> Hi,
> 
> Your subject implies it is about xml serialization, but it is
> marked as [Serializable]. Which "serialization" do you mean?
> XML serialization, or remoting SOAP serialization?
> 
> Atsushi Eno
> 
> 
> Blackskyliner wrote:
>> Hi I just jumped in into mono I hope I'm in the right forum/mailinglist,
>> because i wrote an little server application that I wanted to run on my
>> linux machine. Its originally written in .NET 2.0
>> 
>> To exchange Messages between Server and Client I use an XML-serialized
>> class....
>> In .NET I dont get any problems but with mono I get the following
>> exception:
>> 
>>  There was an error generating the XML document. > The type of the
>> argument
>> object 'ChatClient.serverMessages' is not primitive.
>> 
>> I implemented the serverMessage in the following way:
>> 
>> using System;
>> using System.Text;
>> 
>> namespace ChatClient
>> {
>>     [Serializable]
>>     public class Message
>>     {
>>         public Message_StatusCodes statusCode;
>>         public messageType messageType;
>>         public object message;
>>         public DateTime time;
>>         public string user;
>> 
>>         public Message()
>>         {
>>             init();
>>         }
>> 
>>         public Message(string Username)
>>         {
>>             init();
>>             this.user = Username;
>>         }
>> 
>>         private void init(){
>>             this.statusCode = Message_StatusCodes.NULL;
>>             this.message = null;
>>             this.time = DateTime.Now;
>>             this.user = null;
>>             this.messageType = messageType.NULL;
>>         }
>> 
>>         public void Clear()
>>         {
>>             this.statusCode = Message_StatusCodes.NULL;
>>             this.messageType = messageType.NULL;
>>             this.message = null;
>>             this.time = DateTime.Now;
>>             this.user = null;
>>         }
>> 
>>         public void Clear(string Username)
>>         {
>>             this.Clear();
>>             this.user = Username;
>>         }
>> 
>>         public Message(Message_StatusCodes c)
>>         {
>>             init();
>>             this.statusCode = c;
>>         }
>>     }
>> 
>>     public enum Message_StatusCodes
>>     {
>>         NULL,
>> 
>>         Login,
>> 
>>         UserKicked,
>>         UserJoined,
>>         UserConnected,
>>         UserDisconnected,
>> 
>>         UserListChanged
>>     }
>> 
>>     public enum messageType{
>>         NULL,
>>         UserMessage,
>>         ServerMessage,
>>         ClientMessage
>>     }
>> 
>>     public enum serverMessages
>>     {
>>         LOGIN_ERROR_UserAlreadyExist,
>>         LOGIN_ERROR_UsernameReserved,
>>         LOGIN_OK
>>     }
>> 
>>     public enum clientMessages
>>     {
>>         Disconnect
>>     }
>> }
>> 
>> 
>> Why does this not deserialize in Mono but in C#?? Is there any workaround
>> or
>> smth. else?
>> 
>> I'm thankful about every answer...
>> Blackskyliner
> 
> _______________________________________________
> Mono-list maillist  -  Mono-list at lists.ximian.com
> http://lists.ximian.com/mailman/listinfo/mono-list
> 
> 

-- 
View this message in context: http://www.nabble.com/Problem-with-XML-Serialization-tp24452869p24482404.html
Sent from the Mono - General mailing list archive at Nabble.com.



More information about the Mono-list mailing list