Db2XmlToXElementPropertyHandler

A property handler that maps the Db2 XML type into an XElement property.

A property handler that maps the Db2 XML type into an XElement property.

The XML text is bound as a string, so the parameter has to be accepted by Db2 for the XML column, for example by typing it as DB2Type.Xml via the Db2Type attribute.

Overview

Name Description
Class Db2XmlToXElementPropertyHandler
Namespace RepoDb.PropertyHandlers.Db2
Package RepoDb.Db2
Implements IPropertyHandler<object, XElement>
Column type The Db2 XML type
Property type XElement

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 XElement.Parse(). null, DBNull and an empty value are returned as null.
Set Property to database Converts the XElement into its unformatted XML text via ToString(SaveOptions.DisableFormatting). 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(Db2XmlToXElementPropertyHandler))]
    public XElement 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 = XElement.Parse("<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<Db2XmlToXElementPropertyHandler>(e => e.Content);

Property Level Mapping

To configure via PropertyHandlerMapper:

PropertyHandlerMapper.Add<Document, Db2XmlToXElementPropertyHandler>(e => e.Content, new Db2XmlToXElementPropertyHandler(), true);

Type Level Mapping

To apply the handler to every property of the type XElement:

PropertyHandlerMapper.Add<XElement, Db2XmlToXElementPropertyHandler>(new Db2XmlToXElementPropertyHandler(), true);

See Also