Serializzare un oggetto in SOAP
L'oggetto da serializzare usando C#
I metodi per serializzare e deserializzare su Stream (FileStream in questo caso):
C#
[Serializable]
class Note
{
private string _title;
public string Title
{
get { return _title; }
set { _title = value; }
}
private string _body;
public string Body
{
get { return _body; }
set { _body = value; }
}
private int _version;
public int Version
{
get { return _version; }
set { _version = value; }
}
}
C#
Note note = new Note();
private const string FILENAME = "note.xml";
private void Deserialize()
{
if (File.Exists(FILENAME) == true)
{
//System.Runtime.Serialization.Formatters.Binary.BinaryFormatter sf =
// new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter();
System.Runtime.Serialization.Formatters.Soap.SoapFormatter sf =
new System.Runtime.Serialization.Formatters.Soap.SoapFormatter();
using (Stream strm = File.Open(FILENAME, FileMode.Open))
{
if (strm.Length > 0)
{
try
{
note = (Note)sf.Deserialize(strm);
}
catch (System.Xml.XmlException ex)
{
//MessageBox.Show(ex.Message);
}
}
}
}
}
private void Serialize()
{
//System.Runtime.Serialization.Formatters.Binary.BinaryFormatter sf =
// new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter();
System.Runtime.Serialization.Formatters.Soap.SoapFormatter sf =
new System.Runtime.Serialization.Formatters.Soap.SoapFormatter();
using (Stream strm = File.Open(FILENAME, FileMode.Create, FileAccess.Write))
{
sf.Serialize(strm, note);
}
}
Aggiungere la referenza a System.Runtime.Serialization.Formatters.Soap.dll