DataContract Attribute specifies that the type defines or implements a data contract and is serializable by a serializer.
EnumMember Attribute specifies that the field is an enumeration member and should be serialized.
Example:
namespace WindowsFormsApplication
{public partial class Form1 : Form
{
[DataContract]
public enum Country
{
[EnumMember(Value = "USA")]
USA,
[EnumMember(Value = "DE")]
Germany,
[EnumMember(Value = "FR")]
France,
NotASerializableEnumeration
}
[DataContract (Name = "Customer")]
public class Customer : IExtensibleDataObject{
public Customer(string name, Country cnt)
{
Name = name;
Country = cnt;
}
private ExtensionDataObject extensionDataValue;
public ExtensionDataObject ExtensionData
{
get { return extensionDataValue; }
set { extensionDataValue = value; }
}
[DataMember]
internal string Name;
[DataMember]
internal Country Country;
}
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
Customer p = new Customer("Microsoft", Country.USA );
FileStream fs = new FileStream(@"C:\1.xml", FileMode.Create);
try
{
DataContractSerializer ser = new DataContractSerializer(typeof(Customer));
ser.WriteObject(fs, p);
}
catch (SerializationException exc)
{
MessageBox.Show(exc.Message.ToString());
}
finally
{
fs.Close();
}
}
}
}