Click here to Skip to main content
15,891,684 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi,
I want to serialize a class and send it over tcp. Thats no problem, but I get an exception while deserializing the stream in the client application.
Thats my code:
C#
[Serializable]
internal class SendObj1
{

    public int cmd;
    public string msg;
}

public class SendObj
{
    public int cmd;
    public string msg;
    public byte[] ToByte(int Command)
    {
        SendObj1 obj1 = new SendObj1();
        obj1.cmd = Command;
        XmlSerializer xs = new XmlSerializer(typeof(SendObj1));
        MemoryStream ms = new MemoryStream();
        xs.Serialize(ms, obj1);
        return ms.ToArray();
    }
    public byte[] ToByte(int Command, string Message)
    {
        SendObj1 obj1 = new SendObj1();
        obj1.cmd = Command;
        obj1.msg = Message;
        XmlSerializer xs = new XmlSerializer(typeof(SendObj1));
        MemoryStream ms = new MemoryStream();
        xs.Serialize(ms, obj1);
        return ms.ToArray();
    }
    public SendObj ToObj(byte[] buffer)
    {
        SendObj1 obj = new SendObj1();
        try
        {
            XmlSerializer xs = new XmlSerializer(typeof(SendObj1));
            MemoryStream ms = new MemoryStream();
            obj = (SendObj1) xs.Deserialize(ms);
            msg = obj.msg;
            cmd = obj.cmd;
        }
        catch (Exception e)
        {
        }
        return this;
    }
Posted
Comments
Jibesh 23-Dec-12 14:58pm    
what's the exception you got? Please provide the exception stack for better understanding of the problem
Member 9622894 23-Dec-12 15:13pm    
System.InvalidOperationException was caught
Message=Fehler im XML-Dokument (0,0).
Source=System.Xml
StackTrace:
bei System.Xml.Serialization.XmlSerializer.Deserialize(XmlReader xmlReader, String encodingStyle, XmlDeserializationEvents events)
bei System.Xml.Serialization.XmlSerializer.Deserialize(Stream stream)
bei SendObj.ToObj(Byte[] buffer) in C:\Users\Maxi\documents\visual studio 2010\Projects\Obj\Obj\Class1.cs:Zeile 45.
InnerException: System.Xml.XmlException
Message=Das Stammelement ist nicht vorhanden.
Source=System.Xml
LineNumber=0
LinePosition=0
SourceUri=""
StackTrace:
bei System.Xml.XmlTextReaderImpl.Throw(Exception e)
bei System.Xml.XmlTextReaderImpl.ThrowWithoutLineInfo(String res)
bei System.Xml.XmlTextReaderImpl.ParseDocumentContent()
bei System.Xml.XmlTextReaderImpl.Read()
bei System.Xml.XmlTextReader.Read()
bei System.Xml.XmlReader.MoveToContent()
bei Microsoft.Xml.Serialization.GeneratedAssembly.XmlSerializationReaderSendObj1.Read3_SendObj1()
InnerException:

1 solution

The exception says Root Element is not found. Since your are using XmlSerialization you need to set the RootNode. change the serializable class as below

C#
[Serializable]
[XmlRoot]
public class SendObj1
{
    [XmlElement]
    public int cmd = 0;
    [XmlElement]
    public string msg = string.Empty;
}


/EDIT
The deserialization part is incomplete, you need to pass the buffer to memory stream before deserialize the stream
C#
XmlSerializer xs = new XmlSerializer(typeof(SendObj1));
MemoryStream ms = new MemoryStream(buffer); // this is missing in your code
obj = (SendObj1) xs.Deserialize(ms);
msg = obj.msg;
cmd = obj.cmd;
 
Share this answer
 
v3
Comments
Member 9622894 23-Dec-12 16:23pm    
i have still the same exception
Member 9622894 23-Dec-12 16:26pm    
System.InvalidOperationException was caught
Message=Fehler im XML-Dokument (0,0).
Source=System.Xml
StackTrace:
bei System.Xml.Serialization.XmlSerializer.Deserialize(XmlReader xmlReader, String encodingStyle, XmlDeserializationEvents events)
bei System.Xml.Serialization.XmlSerializer.Deserialize(Stream stream)
bei SendObj.ToObj(Byte[] buffer) in C:\Users\Maxi\documents\visual studio 2010\Projects\Obj\Obj\Class1.cs:Zeile 47.
InnerException: System.Xml.XmlException
Message=Das Stammelement ist nicht vorhanden.
Source=System.Xml
LineNumber=0
LinePosition=0
SourceUri=""
StackTrace:
bei System.Xml.XmlTextReaderImpl.Throw(Exception e)
bei System.Xml.XmlTextReaderImpl.ThrowWithoutLineInfo(String res)
bei System.Xml.XmlTextReaderImpl.ParseDocumentContent()
bei System.Xml.XmlTextReaderImpl.Read()
bei System.Xml.XmlTextReader.Read()
bei System.Xml.XmlReader.MoveToContent()
bei Microsoft.Xml.Serialization.GeneratedAssembly.XmlSerializationReaderSendObj1.Read3_SendObj1()
InnerException:
Jibesh 28-Dec-12 16:20pm    
Check my updated solution for the reason for the exception

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