DataContractSerializer

 

Limitations

- There is currently a limitation to the parameters you can pass to the DataContract and DataMember attributes. In fact, you can use the DataContract and DataMember attributes normally, but if you need to specify some additional properties, such as the Name in [DataContract(Name="TheName")], it will not work because JSIL is currently unable to retain those properties (it only retains the constructor parameters of the attributes). Therefore, instead, you need to use the "DataContract2" and "DataMember2" classes, and pass the parameters to their constructor, like this: [DataContract2(TheName: "TheName")]. Notice that we use the colons (":") instead of the equal sign ("=") because we are passing constructor named arguments instead of directly setting the properties of the attribute. We are working to fix this limitation. If you need more information, please contact us.

 

Tutorial

Please use the following code to serialize an object with the DataContractSerializer:

var dataContractSerializer = new DataContractSerializer(typeof(ClassToSerialize));
var xml = dataContractSerializer.SerializeToString(_instanceToSerialize);

Please use the following code to deserialize an object with the XmlSerializer:

var dataContractSerializer = new DataContractSerializer(typeof(Person));
ClassToSerialize deserializedObject = (ClassToSerialize)dataContractSerializer.DeserializeFromString(xml);

 

Full example:

[DataContract]
public class Person
{
    public void Init()
    {
        this.FullName = "John Doe";
        this.Age = 30;
        this.MailingAddress = "mailing@mail.com";
        this.TelephoneNumber = "0123456789";
    }

    // This member is serialized.
    [DataMember]
    internal string FullName;

    // This is serialized even though it is private.
    [DataMember]
    private int Age;

    // This is not serialized because the DataMemberAttribute 
    // has not been applied.
    private string MailingAddress;

    // This is not serialized, but the property is.
    private string telephoneNumberValue;

    [DataMember]
    public string TelephoneNumber
    {
        get { return telephoneNumberValue; }
        set { telephoneNumberValue = value; }
    }
}

public void Serialize()
{
    var person = new Person();
    person.Init();

    var dataContractSerializer = new DataContractSerializer(typeof(Person));
    var xml = dataContractSerializer.SerializeToString(person);
    MessageBox.Show(xml);
}

public void Deserialize()
{
    string xml = @"<?xml version=""1.0"" encoding=""UTF-8""?>
<Person xmlns=""http://schemas.datacontract.org/2004/07/"" xmlns:i=""http://www.w3.org/2001/XMLSchema-instance"">
<Age>30</Age>
<FullName>John Doe</FullName>
<TelephoneNumber>0123456789</TelephoneNumber>
</Person>";
    var dataContractSerializer = new DataContractSerializer(typeof(Person));
    var person = (Person)dataContractSerializer.DeserializeFromString(xml);
    MessageBox.Show("Person name: " + person.FullName + Environment.NewLine + "Person phone number: " + person.TelephoneNumber);
}

 

Support

For any question, please post a message on the forums or contact us at support@cshtml5.com