序列化:如何在c#中反序列化

本文关键字:反序列化 序列化 | 更新日期: 2023-09-27 18:10:45

如何根据我在此方法中所做的工作对字符串进行反序列化?基本上,我在这里所做的是通过网络传递字符串,使用序列化和反序列化字符串来传递消息。但是,一旦我设法收到消息,我不知道我所做的是正确的。下面是代码:

string ConvertToString(FrogGame np, Frog1 pm, Frog2 pm2) //Serialization. the three parameters are the classes. 
        {
            XmlSerializer sendSerializer = new XmlSerializer(typeof(FrogGame),new Type[]{typeof(Frog1),typeof(Frog2)});
            StreamWriter myWriter = new StreamWriter(@"pad1.xml");
            sendSerializer.Serialize(myWriter, np);
            sendSerializer.Serialize(myWriter, pm);
            sendSerializer.Serialize(myWriter, pm2);
            return myWriter.ToString();
        } //Overall, I serialize it into string

通过网络传递字符串后,我想对其进行反序列化,以便将消息传递给类。我该如何继续呢?我怎么编辑?代码:

void StringReceived(string str) //so str is myWriter.ToString()
        {
            XmlSerializer revSerializer = new XmlSerializer(typeof(FrogGame), new Type[] { typeof(Frog1), typeof(Frog2) });
            FileStream myFileStream = new FileStream(@"pad1.xml", FileMode.Open);
            FrogGame b = (FrogGame)revSerializer.Deserialize(myFileStream);
            if (b is Frog1) 
            {
                if (Network.IsServer())
                {
                    pm = (Frog1)b;
                    pm.Position.Y = b.pm.Position.Y;
                    pm.Position.X = b.pm.Position.X;
                }
                else
                {
                    System.Diagnostics.Debug.WriteLine("BAD Message: " + msg);
                }
            }
            else if (b is Frog2)
            {
                if (Network.IsClient())
                {
                    pm2 = (PaddleMessage2)b;
                    pm2.Position.Y = b.pm2.Position.Y;
                    pm2.Position.X = b.pm2.Position.X;
                }
                else
                {
                    System.Diagnostics.Debug.WriteLine("BAD Message: " + msg);
                }
            }
        }

序列化:如何在c#中反序列化

我可能误解了你的问题,但我为什么不把你想要保存在一个类中的所有东西,这样做(另外,如果你使用类,你的数据"传输"answers"管理"将更容易):

序列化

XmlSerializer serializer = new XmlSerializer(typeof(FrogGameData));
TextWriter textWriter = new StreamWriter("FrogGameSaveFile.xml");
serializer.Serialize(textWriter, _frogGameData);
textWriter.Close();

反序列化

XmlSerializer deserializer = new XmlSerializer(typeof(FrogGameData));
TextReader textReader = new StreamReader("FrogGameSaveFile.xml");
_frogGameData = (FrogGameData)deserializer.Deserialize(textReader);
textReader.Close();

注意:需要保存的字段应该有属性,因为XML中的标记将模仿属性名称。

附加注意: FrogGameData与这样的自动序列化的普通类没有什么不同。XML将模仿类中的属性顺序,使其与XML文件中的属性顺序相同。

但是如果你想要重新安排XML标签的位置,你可以在你的属性上做一些像[XmlElement(Order = 1)], [XmlElement(Order = 2)]等来自定义XML文件中的顺序。


如果你需要它,这是一个FrogGameData类的例子:

public class FrogGameData
{
    private Frog _frog1;
    private Frog _frog2;
    public Frog Frog1
    {
        get { return _frog1; }
        set { _frog1 = value; }
    }
    public Frog Frog2
    {
        get { return _frog2; }
        set { _frog2 = value; }
    }
}

XML就像这样:

<?xml version="1.0" encoding="utf-8"?>
<FrogGameData>
    <Frog1>Something-depends-on-your-data</Frog1>
    <Frog2>Something-depends-on-your-data</Frog2>
</FrogGameData>

但是,如果您的类是(注意XmlElement部分):

public class FrogGameData
{
    private Frog _frog1;
    private Frog _frog2;
    [XmlElement(Order = 2)]
    public Frog Frog1
    {
        get { return _frog1; }
        set { _frog1 = value; }
    }
    [XmlElement(Order = 1)]
    public Frog Frog2
    {
        get { return _frog2; }
        set { _frog2 = value; }
    }
}

那么,您的XML将是:

<?xml version="1.0" encoding="utf-8"?>
<FrogGameData>
    <Frog2>Something-depends-on-your-data</Frog2>
    <Frog1>Something-depends-on-your-data</Frog1>
</FrogGameData>