Db2XmlToXmlDocumentPropertyHandler
A property handler that maps the Db2 XML type into an XmlDocument property.
A property handler that maps the Db2 XML type into an XmlDocument property.
The XML text is bound as a
string, so the parameter has to be accepted by Db2 for theXMLcolumn, for example by typing it asDB2Type.Xmlvia the Db2Type attribute.
Overview
| Name | Description |
|---|---|
| Class | Db2XmlToXmlDocumentPropertyHandler |
| Namespace | RepoDb.PropertyHandlers.Db2 |
| Package | RepoDb.Db2 |
| Implements | IPropertyHandler<object, XmlDocument> |
| Column type | The Db2 XML type |
| Property type | XmlDocument |
Conversions
| Method | Direction | Description |
|---|---|---|
| Get | Database to property | Converts the value returned by the driver (a DB2Xml or a string) into XML text and parses it via XmlDocument.LoadXml(). null, DBNull and an empty value are returned as null. |
| Set | Property to database | Converts the XmlDocument into its XML text via OuterXml. A null value is written as null. |
Exceptions
| Exception | Thrown When |
|---|---|
ArgumentException |
The type of the value returned by the driver is not supported. |
Usability
Bind the handler to the property that maps to an XML column, and type the parameter as DB2Type.Xml.
Attribute
public class Document
{
public int Id { get; set; }
[Map("content"), Db2Type(DB2Type.Xml), PropertyHandler(typeof(Db2XmlToXmlDocumentPropertyHandler))]
public XmlDocument Content { get; set; }
}
Once bound, the library invokes the handler automatically when the property is read or written.
using (var connection = new DB2Connection(connectionString))
{
var content = new XmlDocument();
content.LoadXml("<root><item id=\"1\" /></root>");
connection.Insert(new Document { Id = 1, Content = content });
var document = connection.Query<Document>(e => e.Id == 1).First();
}
Fluent Mapping
To configure via FluentMapper:
FluentMapper
.Entity<Document>()
.PropertyHandler<Db2XmlToXmlDocumentPropertyHandler>(e => e.Content);
Property Level Mapping
To configure via PropertyHandlerMapper:
PropertyHandlerMapper.Add<Document, Db2XmlToXmlDocumentPropertyHandler>(e => e.Content, new Db2XmlToXmlDocumentPropertyHandler(), true);
Type Level Mapping
To apply the handler to every property of the type XmlDocument:
PropertyHandlerMapper.Add<XmlDocument, Db2XmlToXmlDocumentPropertyHandler>(new Db2XmlToXmlDocumentPropertyHandler(), true);