How to map XML type column to a strongly typed object property with NHibernate?

nhibernate, sql, xml, xml-serialization

Solution

You need to write an `IUserType` that takes care of the conversion.

You could start from XmlDocType, which is the one actually converting from raw XML to a XmlDocument.

Problem

I have the following table: ``` CREATE TABLE [dbo].[Data] ( [Id] UNIQUEIDENTIFIER NOT NULL, [Data] XML NOT NULL, ); ``` I need to map it to the object: ``` class Data { public virtual Guid Id {get; set;} public virtual StronglyTypedData Data {get; set;} } ``` Where, StronglyTypedData is something like: ``` class StronglyTypedData { public string Name {get; set;} public int Number {get; set;} } ``` By default, XML columns are mapped to XmlDocument properties, but I would like XML serialization/deserialization to StronglyTypedData property to happen instead at mapping time. What do I need to do to accomplish this?

Original source