0

我正在尝试使用 DataContractSerializer 反序列化文件。我有这样的课:

[DataContract]
public class kontenerUstawienia
{
    [DataMember]
    public int[] stanGry;
    [DataMember]
    public int maxSize;
    [DataMember]
    public int[] stanOpcji;

    [DataMember]
    public int numerFlagi1;
    [DataMember]
    public int numerFlagi2;

    public kontenerUstawienia()
    {
    }

(...)
}

在内部,将序列化的类实例保存到文件后,我读取了文件并尝试反序列化:

try
{
    zapiszObiektUstawien((kontenerUstawienia)deserializer.ReadObject(strumien));
}

catch (SerializationException e)
{
    System.Diagnostics.Debug.WriteLine("\n\n\n\n++++++\n" + 
    e.Message 
    + "\n+++++++++++++++++++++++++++++++++++++++++++++++");
}

catch 打印我:

++++++
There was an error deserializing the object of type
WindowsPhoneGame1.kontenerUstawienia. There are multiple root elements. Line 1,
position 599.
+++++++++++++++++++++++++++++++++++++++++++++++

我究竟做错了什么?

编辑:这是我序列化并保存的代码:

public void zapiszDoPliku(string sciezkaDoPliku, IsolatedStorageFile katalog)
{
    IsolatedStorageFileStream strumien = katalog.CreateFile(sciezkaDoPliku); // tworzenie pliku

    MemoryStream ms = new MemoryStream();
    StreamReader r  = new StreamReader(ms);

    DataContractSerializer serializer = new DataContractSerializer(typeof(kontenerUstawienia));

    serializer.WriteObject(ms, this);

    ms.Position = 0;

    string daneDoZapisania = r.ReadToEnd();

    byte[] bytes = Encoding.Unicode.GetBytes(daneDoZapisania);

    strumien.Write(bytes, 0, bytes.Length);

    ms.Close();
    strumien.Close();
}

编辑2:

文件已保存: 文件在这里

4

1 回答 1

1

您确定文件在写入之前为空吗?

于 2013-03-21T11:21:50.537 回答