0

我正在尝试创建一个在线跳棋游戏来开始学习网络。我有一个服务器/客户端架构,我正在使用 TCP 套接字。我想object通过流发送自定义。我使用 XML 序列化系统来做到这一点,并且一切似乎都可以正常工作,直到客户端需要对来自服务器的“序列化”消息进行反序列化。达到这一点时,我收到此错误:

XmlException:文档元素未出现。第 1 行,位置 1。 Mono.Xml2.XmlTextReader.Read () System.Xml.XmlTextReader.Read () System.Xml.XmlReader.MoveToContent () System.Xml.Serialization.XmlSerializationReaderInterpreter.ReadRoot () System.Xml.Serialization。 XmlSerializer.Deserialize(System.Xml.Serialization.XmlSerializationReader 阅读器)

这是我用于序列化对象的函数:

private string Serialize(List<PieceInfo> pieces)
{

    StreamWriter stWriter = null;
    XmlSerializer xmlSerializer;
    string buffer;
    try
    {
        xmlSerializer = new XmlSerializer(typeof(List<PieceInfo>));
        MemoryStream memStream = new MemoryStream();
        stWriter = new StreamWriter(memStream);
        XmlSerializerNamespaces xs = new XmlSerializerNamespaces();
        xs.Add("", "");
        xmlSerializer.Serialize(stWriter, pieces, xs);
        buffer = Encoding.ASCII.GetString(memStream.GetBuffer());
        return buffer;
    }


    catch (Exception Ex)
    {
        throw Ex;
    }    
}

这是我用于反序列化的函数:

public List<PieceInfo> DeSerialize(string xmlString) 
{
    XmlSerializer xmlSerializer;
    MemoryStream memStream = null;
    try
    {
        xmlSerializer = new XmlSerializer(typeof(List<PieceInfo>));            

        byte[] bytes = new byte[xmlString.Length];
        Encoding.ASCII.GetBytes(xmlString, 0, xmlString.Length, bytes,0);
        memStream = new MemoryStream(bytes);
        memStream.Seek(0, SeekOrigin.Begin);
        object objectFromXml = xmlSerializer.Deserialize(memStream);
        List<PieceInfo> pieces = (List<PieceInfo>)objectFromXml;
        return pieces;
    }
    catch (Exception Ex)
    {
        throw Ex;
    }
} 

发送消息代码:

StreamWriter writer = new StreamWriter(tcpClient.GetStream());
writer.WriteLine("GRABB~"+ Serialize(ListPiecesForSerilization));
writer.Flush();

接收消息代码:

private void OnIncomingData(string data)
{
    string[] lines;
    lines = data.Split('~');
    for (int i = 0; i != lines.Length; i++)
    {
        switch (lines[i])
        {                                          
            case "GRABB": // get board's current condition
                List<PieceInfo> pieces = DeSerialize(lines[i+1]);
                board.SetPiecesPosition(pieces);
                break;
        }
            //Debug.Log(data);
    }
}

PieceInfo类,包含有关作品的基本信息:

[Serializable]
public class PieceInfo 
{
    public int xPos;
    public int zPos;
    public bool isWhite;
    public bool isKing;

    public void SetPieceInfo(int x, int z, bool c, bool k)
    {
        xPos = x;
        zPos = z;
        isWhite = c;
        isKing = k;
    }
}

该类PieceInfo标有属性[Serializable],但我没有在其中设置任何其他属性。

我将 xml 消息作为stringusingNetworkStreamStreamWriter/发送StreamReader

为什么我在尝试反序列化消息时会收到错误消息?

4

1 回答 1

0

问题是我正在使用StreamWriter.WriteLine并且StreamReader.ReadLine()没有写入/读取洞 xml 文件。我StreamReader.Read()在循环中使用,直到满足我设置为“ end-of-file”标记的符号。

于 2018-03-21T11:45:49.633 回答