I'm trying to send a list<clase_A>
by xmlSerializer
, from an interface to unity.
Here is my server code:
using (connectedTcpClient = tcpListener.AcceptTcpClient())
{
using (NetworkStream stream = connectedTcpClient.GetStream())
{
int length;
XmlSerializer xmls = new XmlSerializer(typeof(List<Clase_comando>));
while ((stream.CanRead && (length = stream.Read(bytes, 0, bytes.Length)) != 0))
{
List<Clase_comando> com = null;
var b = new byte[10000];
com = (List<Clase_comando>)xmls.Deserialize(stream);
//Here i get the problem
}
}
}
Here is the code of Visual Studio:
NetworkStream stream = socketConnection.GetStream();
StreamWriter sw = new StreamWriter(stream);
sw.AutoFlush = true;
XmlSerializer xmls = new XmlSerializer(typeof(List<Clase_A>));
if (stream.CanWrite){
z = My list
byte[] b = new byte[10000];
Stream st = new MemoryStream(b);
xmls.Serialize(st, z);
stream.Write(b, 0, b.Length);
stream.Dispose();
}
The problem is when I receive the interface data in unity, it trow this exception:
XmlException: Root element is missing. System.Xml.XmlTextReaderImpl.Throw (System.Exception e) (at <7fd195060d8c41448694ab221d3b56ca>:0) System.Xml.XmlTextReaderImpl.ThrowWithoutLineInfo (System.String res) (at <7fd195060d8c41448694ab221d3b56ca>:0)
In this line
while ((stream.CanRead && (length = stream.Read(bytes, 0, bytes.Length)) != 0))
data is read from the stream
to the bytes
.
After that, there is either no data in the stream at all either the root is already missing.
So on the next line, the serializer reads from the corrupted stream:
com = (List<Clase_comando>)xmls.Deserialize(stream);
Try this code:
using (NetworkStream stream = connectedTcpClient.GetStream())
{
var xmls = new XmlSerializer(typeof(List<Clase_comando>));
var com = (List<Clase_comando>)xmls.Deserialize(stream);
}