Cannot deserialize ActiveRecord object due to IList<T>

70 views
Skip to first unread message

Michael

unread,
Mar 19, 2012, 7:32:34 PM3/19/12
to Castle Project Users
Active record requires collections to be IList<T>, which I can't seem
to deserialize. Does anyone have any suggestions to modify my AR
classes (or serialization/deserialization code) to allow me to
serialize/deserialize these AR classes?

[Serializable]
[DataContract]
[ActiveRecord("Patients")]
public class Patient : ActiveRecordBase<Patient>
{
[DataMember]
[PrimaryKey(Generator = PrimaryKeyType.Identity, Column =
"PatientId")]
public virtual long Id { get; set; }

[DataMember]
[Property(NotNull = true, Length = 100)]
public virtual string First { get; set; }

[DataMember]
[Property(NotNull = true, Length = 100)]
public virtual string Last { get; set; }

[DataMember]
[XmlArray]
[HasMany(typeof(Address), Cascade =
ManyRelationCascadeEnum.AllDeleteOrphan, Table = "Addresses",
ColumnKey = "PatientId", Inverse = true)]
public virtual IList<Address> Addresses { get; set; }
}

[Serializable]
[DataContract]
[ActiveRecord("Addresses")]
public class Address : ActiveRecordBase<Address>
{
[DataMember]
[PrimaryKey(Generator = PrimaryKeyType.Identity, Column =
"AddressId")]
public virtual long Id { get; set; }

[DataMember]
[Property(NotNull = true, Length = 255)]
public virtual string Address1 { get; set; }

[DataMember]
[Property(NotNull = true, Length = 255)]
public virtual string City { get; set; }

[DataMember]
[Property(NotNull = true, Length = 10)]
public virtual string PostalCode { get; set; }

[DataMember]
[Property(NotNull = true, Length = 50)]
public virtual string State { get; set; }
}

Here's my code to serialize & deserialize:

public static string Serialize(object objectToSerialize)
{
var ns = new XmlSerializerNamespaces();
ns.Add(String.Empty, String.Empty);

var memoryStream = new MemoryStream();
var settings = new XmlWriterSettings {Indent = false,
OmitXmlDeclaration = true, Encoding = Encoding.UTF8};

var serializer = new XmlSerializer(typeof(T));
var writer = XmlWriter.Create(memoryStream, settings);
serializer.Serialize(writer, objectToSerialize, ns);

return Utf8ByteArrayToString(memoryStream.ToArray());
}

public static object Deserialize(string objectXml, Type[]
additionalTypes)
{
var encoding = new UTF8Encoding();
var byteArray = encoding.GetBytes(objectXml);

var memoryStream = new MemoryStream(byteArray);
var xmlRoot = XElement.Load(memoryStream);

var o = new XmlSerializer(typeof(T),
additionalTypes).Deserialize(xmlRoot.CreateReader());
return o;
}

Thanks!

Mauricio Scheffer

unread,
Mar 19, 2012, 7:37:22 PM3/19/12
to castle-pro...@googlegroups.com
Maybe this can help :   http://stackoverflow.com/questions/1958684/nhibernate-how-do-i-xmlserialize-an-isett 

Cheers,
Mauricio



--
You received this message because you are subscribed to the Google Groups "Castle Project Users" group.
To post to this group, send email to castle-pro...@googlegroups.com.
To unsubscribe from this group, send email to castle-project-u...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/castle-project-users?hl=en.


Michael

unread,
Mar 19, 2012, 10:51:42 PM3/19/12
to Castle Project Users
Fantastic! Using the DataContractSerializer solved my problem.

Many thanks!

On Mar 19, 6:37 pm, Mauricio Scheffer <mauricioschef...@gmail.com>
wrote:
> Maybe this can help :http://stackoverflow.com/questions/1958684/nhibernate-how-do-i-xmlser...
Reply all
Reply to author
Forward
0 new messages