NHibernate: How do I XmlSerialize an ISet<T>?

Solution 1:

NHibernate serialization has been treated a lot on stackoverflow. See:

  • C# Castle ActiveRecord: How to elegantly (XML) serialize ActiveRecord objects?
  • How do I serialize all properties of an NHibernate-mapped object?
  • NHibernate and WCF Serialization(Unidirectional)
  • JSON.NET and nHibernate Lazy Loading of Collections
  • Which .NET JSON serializers can deal with NHibernate proxy objects?
  • DTOs vs Serializing Persisted Entities
  • Returning NHibernate mapping classes from WCF services

Bottom line: use DTOs.

Solution 2:

Try using the DataContractSerializer instead. It's more restrictive, but will serialize more.

Dan Rigsby explains the difference between XMLSerializer and DataContractSerializer

Here's an example from one of my posts on stackoverflow:

public XDocument GetProductXML(Product product)
    {
        var serializer = new DataContractSerializer(typeof(Product));
        var document = new XDocument();

        using (var writer = document.CreateWriter())
        {
            serializer.WriteObject(writer, product);
            writer.Close();
        }

        return document;
    }

Solution 3:

You can never XML Serialize an interface - only a concrete class that implements the interface.