2013-04-26 15 views
0

在我的項目上工作。我必須做數據序列化。如何在C#中進行數據序列化?

我有一個代碼,但我不知道如何在其上應用序列化。

public class allmethods 
{ 
    private static string Name; 
    private static int ID; 
    private static int Age; 
    private static string Email; 
    private static string output; 

       public static void WritingMethod() 
      { 
       int count = 0; 
       while (count < 10) 
      { 
        Console.Write(" Enter your Name: "); 
        Name = Console.ReadLine(); 

        Console.Write(" Enter your ID: "); 
        ID = int.Parse(Console.ReadLine()); 

        Console.Write(" Enter your Age: "); 
        Age = int.Parse(Console.ReadLine()); 

        Console.Write(" Enter your E-mail: "); 
        Email = Console.ReadLine(); 



        StreamWriter Sw = new StreamWriter("fileone.txt", true); 
        string output = string.Format("Thank you for registration! Your Submitted information are:" + Environment.NewLine + "Name: {0}" 
        + Environment.NewLine + "ID: {1}" + Environment.NewLine + "Age: {2}" + Environment.NewLine + "E-mail: {3}", Name, ID, Age, Email); 
        Console.WriteLine(output);  
        Sw.WriteLine(output + Environment.NewLine); 
        Console.ReadLine(); 

        Sw.Close(); 
        count++; 
      } 
     } 
    } 

如果有人幫助我,我會很高興。謝謝。

+0

你想什麼連載以此爲 – 2013-04-26 06:06:03

+0

一個簡單的,低掛水果入手:C#和DOM(XML序列化) :HTTP:// w^ww.mastercsharp.com/Article/69/working-with-xml-dom – Najzero 2013-04-26 06:06:46

回答

1

序列化和反序列化是一個非常大的話題,並不適合單個問題。

但是,既然你問從哪裏開始,我建議你研究一下ISerializable接口。

http://msdn.microsoft.com/en-CA/library/system.runtime.serialization.iserializable.aspx

祝你好運。

+0

用於ISerializable指針的+1。但是當你希望他「祝你好運」時,我感覺到了一股強大的干擾 - 編程不應該依靠運氣(主要是) – Najzero 2013-04-26 06:09:43

+0

更多信息:['DataContracts'](http://msdn.microsoft.com/en-us /library/ms733127.aspx)可能是實現'ISerializable'時最好的開始。 – 2013-04-26 06:09:55

+0

@Najzero這是一個表達。我不認爲我曾經聽過有人說「工作得很好」。或者「你的努力應該得到回報。」在談論工作時。 – LightStriker 2013-04-26 06:10:50

1

看看這些examples從一本書C#在一個果殼。閱讀序列化章節也有幫助。

1

你需要做的第一件事就是擺脫那些靜態字段;大多數串行對對象的工作......所以讓我們創建一個class

public class Person 
{ 
    public string Name { get; set; } 
    public int ID { get; set; } 
    public int Age { get; set; } 
    public string Email { get; set; } 
} 

所以我們的主要邏輯就變成:

var person = new Person(); 
Console.Write(" Enter your Name: "); 
person.Name = Console.ReadLine(); 

Console.Write(" Enter your ID: "); 
person.ID = int.Parse(Console.ReadLine()); 

Console.Write(" Enter your Age: "); 
person.Age = int.Parse(Console.ReadLine()); 

Console.Write(" Enter your E-mail: "); 
person.Email = Console.ReadLine(); 

接下來我們要問 - 「做我們想要的序列化格式?」。它看起來像你正在追加。通常我會說「從XmlSerializer或JSON開始」,但這些都不可追加。國際海事組織普遍是一個不好的選擇(極少數例外)。我有點偏見,但一個一個簡單易用的可附加序列化格式的例子是協議緩衝區,所以我會使用它 - 請注意,這是一種二進制格式,但易於使用。我們需要做的第一件事是添加(可能通過visual studio中的nuget包管理器)protobuf-net。然後,我們需要告訴它如何處理我們的類型,這是我們的註釋做:

[ProtoContract] 
public class Person 
{ 
    [ProtoMember(1)] public string Name { get; set; } 
    [ProtoMember(2)] public int ID { get; set; } 
    [ProtoMember(3)] public int Age { get; set; } 
    [ProtoMember(4)] public string Email { get; set; } 
} 

現在我們改變我們寫代碼,使用串行(以追加方式):

using (var file = File.Open("fileone.bin", FileMode.Append)) 
{ 
    Serializer.SerializeWithLengthPrefix(file, person, 
     PrefixStyle.Base128, Serializer.ListItemTag); 
} 

和然後,以證明我們可以在最後讀回:

using (var file = File.OpenRead("fileone.bin")) 
{ 
    foreach(var person in Serializer.DeserializeItems<Person>(
     file, PrefixStyle.Base128, Serializer.ListItemTag)) 
    { 
     Console.WriteLine("{0}, {1}, {2}, {3}", 
      person.ID, person.Name, person.Age, person.Email); 
    } 
} 
相關問題