这是Ayende RahienNHibernate所做的一个扩展,让NHibernate可以保存XML类型的数据到数据库中,这在某些情况下非常有用。可以把XML转换为XmlDocument保存,也可以直接把对象序列化成XML保存到数据库中。看一下简单的使用过程:

业务实体类

NHibernate and XML Column Typepublic class Document
}

映射文件,注意xml属性的类型,下载的代码中有XmlType的实现。

NHibernate and XML Column Type<?xml version='1.0' encoding='utf-8'?>
NHibernate and XML Column Type
NHibernate and XML Column Type
<hibernate-mapping
NHibernate and XML Column Type
NHibernate and XML Column Type     
xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'
NHibernate and XML Column Type
NHibernate and XML Column Type     
xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns='urn:nhibernate-mapping-2.0'>
NHibernate and XML Column Type
NHibernate and XML Column Type  
<class name='NHibernate.Xml.Document,eXmlDataType2003' table='Documents'>
NHibernate and XML Column Type
NHibernate and XML Column Type    
<id name='Id'>
NHibernate and XML Column Type
NHibernate and XML Column Type      
<generator class='identity'/>
NHibernate and XML Column Type
NHibernate and XML Column Type    
</id>
NHibernate and XML Column Type
NHibernate and XML Column Type    
<property name='Author'/>
NHibernate and XML Column Type
NHibernate and XML Column Type    
<property name='Xml' column='xml' type='NHibernate.Xml.XmlType, XmlDataType2003' />
NHibernate and XML Column Type
NHibernate and XML Column Type  
</class>
NHibernate and XML Column Type
NHibernate and XML Column Type
</hibernate-mapping>

客户程序使用

NHibernate and XML Column Typepublic class Program
}
数据库中的结果

 

最后把所有的感谢都给Ayende Rahien仁兄吧。

完整源码下载

.NET1.1版本:/Files/Terrylee/NHibernate.Xml-Sample2003.zip[Mircea Jivoin提供]

.NET2.0版本:/Files/Terrylee/NHibernate.Xml-Sample2005.zip[Ayende Rahien提供]

相关文章: