2013-04-02 18 views
6

Tôi đang cố gắng deserialize một đối tượng tôi lưu vào một tập tin (với Binary Formatter). Dù tôi cố gắng, tôi nhận được ngoại lệ: Kết thúc Suối gặp phải trước khi phân tích cú pháp được hoàn thànhđịnh dạng deserialize cho: Kết thúc Stream gặp phải trước khi phân tích cú pháp được hoàn thành

Tôi có dòng mã sau đây:

public static T DeserializeFromBinaryFile<T>(string fileName) 
{ 
    T instance = default(T); 
    FileStream fs = new FileStream(fileName, FileMode.Open); 
    try 
    { 
     BinaryFormatter formatter = new BinaryFormatter(); 
     instance = (T)formatter.Deserialize(fs); 
    } 
    catch{} 
    finally 
    { 
     fs.Close(); 
    } 

    return instance; 
} 

Tôi cũng đã cố gắng:

public static T DeserializeFromBinaryFile<T>(string fileName) 
{ 
    T instance = default(T); 
    FileStream fs = new FileStream(fileName, FileMode.Open, FileAccess.Read); 
    MemoryStream ms = new MemoryStream(); 
    try 
    { 
     byte[] bytes = new byte[fs.Length]; 
     fs.Read(bytes, 0, (int)fs.Length); 
     ms.Write(bytes, 0, (int)fs.Length); 
     ms.Position = 0; 
     ms.Seek(0, SeekOrigin.Begin); 
    } 
    catch { } 

    try 
    { 
     BinaryFormatter formatter = new BinaryFormatter(); 
     instance = (T)formatter.Deserialize(ms); 
    } 
    catch { } 
    finally 
    { 
     ms.Close(); 
     fs.Close(); 
    } 

    return instance; 
} 

Nhưng bất cứ điều gì tôi làm, luôn có ngoại lệ:

Kết thúc luồng gặp phải trước khi phân tích cú pháp được hoàn thành

ADDITION: Tôi vừa thực hiện thêm một số thử nghiệm. Nếu tôi có một đối tượng đơn giản, chỉ một vài thuộc tính, nó hoạt động tốt. Sử dụng một đối tượng phức tạp hơn (lớn), có các đối tượng khác, enums, vv đóng gói, đó là khi tôi nhận được ngoại lệ.

Trả lời

1

Cố gắng thiết lập các position-0 cho luồng của mình, bên trong khối try/catch thứ hai:

BinaryFormatter b = new BinaryFormatter(); 
s.Position = 0; 
return (YourObjectType)b.Deserialize(s); 
+1

Tôi đã không, không được, vẫn cung cấp cho các ngoại lệ. – royu

0

có bạn đã cố gắng để deserialize các FileStream chứ không phải là MemoryStream? nó hoạt động cho tôi.

FileStream fs = new FileStream(fileName, FileMode.Open); 
fs.position=0; 
instance = (T)formatter.Deserialize(fs); 

liên quan

0

Mỗi thành viên trong lớp để được tuần tự, phải có các thuộc tính [Serializable()] hoặc được đánh dấu [NonSerialized]. Lưu ý, enums và tất cả các loại được xây dựng đơn giản đã được tuần tự hóa và không yêu cầu thuộc tính [Serializable()].

Ví dụ bên dưới cho cùng một lỗi. Bỏ ghi chú số [Serializable()] ở trên public class B và quá trình deserialization sẽ xảy ra mà không có lỗi.

* Không thể nói chắc chắn xem đây có phải là nguyên nhân gây ra lỗi của bạn hay không vì bạn không cung cấp mẫu của lớp dẫn đến lỗi.

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.Threading.Tasks; 
using System.IO; 
using System.Runtime.Serialization; 
using System.Runtime.Serialization.Formatters.Binary; 

//[Serializable()] 
public class B 
{ 
    public int y = 2; 
} 
[Serializable()] 
public class A 
{ 
    public int x = 2; 
    /*[NonSerialized]*/ public B b; 
    public static T DeserializeFromBinaryFile<T>(string fileName) 
    { 
     T instance = default(T); 
     FileStream fs = new FileStream(fileName, FileMode.Open); 
     try 
     { 
      BinaryFormatter formatter = new BinaryFormatter(); 
      instance = (T)formatter.Deserialize(fs); 
     } 
     catch (System.Exception ex) 
     { 
      Console.WriteLine(ex.Message); 
     } 
     finally 
     { 
      fs.Close(); 
     } 

     return instance; 
    } 
    public static void SerializeBinaryFile<T>(string fileName,T t) 
    { 
     try 
     { 
      using (FileStream fs = File.Open(fileName, FileMode.Create)) 
      { 
       BinaryFormatter bf = new BinaryFormatter(); 
       bf.Serialize(fs, t); 
      } 
     } 
     catch (System.Exception ex) 
     { 

     } 
    } 
} 

class Program 
{ 
    static void Main(string[] args) 
    { 
     A testA = new A(); 
     A.SerializeBinaryFile("test.dat", testA); 
     A testReadA = A.DeserializeFromBinaryFile<A>("test.dat"); 
    } 
} 

https://msdn.microsoft.com/en-us/library/ms973893.aspx