Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
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:
[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 23-Dec-12 8:48am
Comments
jibesh at 23-Dec-12 14:58pm
   
what's the exception you got? Please provide the exception stack for better understanding of the problem
Member 9622894 at 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

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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
 
[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
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;
  Permalink  
v3
Comments
Member 9622894 at 23-Dec-12 16:23pm
   
i have still the same exception
Member 9622894 at 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 at 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)

  Print Answers RSS
0 OriginalGriff 5,374
1 Sergey Alexandrovich Kryukov 4,713
2 Peter Leow 2,944
3 DamithSL 2,465
4 Maciej Los 2,270


Advertise | Privacy | Mobile
Web01 | 2.8.140718.1 | Last Updated 28 Dec 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid