using System.IO;
using System.Runtime.Serialization.Formatters.Soap;
namespace C16_2
{
[Serializable]
public class Login
{
private string s_username;
private string s_passworld;
[NonSerialized]
private string s_userid;
public string Username
{
get { return s_username; }
set { this.s_username = value; }
}
public string PassWorld
{
get { return s_passworld; }
set { this.s_passworld = value; }
}
public string UserID
{
get { return s_userid; }
set { this.s_userid = value; }
}
}
public class Test
{
///
///序列化
///
public void SopFormatterSerialize()
{
Login login = new Login();
login.Username = "admin";
login.PassWorld = "123456789";
login.UserID = "s2";
FileStream fileStream = new FileStream("c:\\User.xml",FileMode.Create);
SoapFormatter b = new SoapFormatter();
b.Serialize(fileStream,login);
fileStream.Close();
}
///
///反序列化
///
public void SoapFormatterDeSerialize()
{
Login login = new Login();
login.Username = "Jim";
FileStream fileStream = new FileStream("c:\\User.xml",FileMode.Open,FileAccess.Read,FileShare.Read);
SoapFormatter b = new SoapFormatter();
login = b.Deserialize(fileStream) as Login;
Console.WriteLine(login.Username);
Console.WriteLine(login.PassWorld);
Console.WriteLine(login.UserID);
fileStream.Close();
}
public static void Main()
{
Test test = new Test();
test.SopFormatterSerialize();
test.SoapFormatterDeSerialize();
}
}
}
沒有留言:
張貼留言