XmlToXDocumentPropertyHandler
A property handler that maps the SQL Server xml type into an XDocument property.
A property handler that maps the SQL Server xml type, which the database returns as a string, into an XDocument property.
Overview
| Name | Description |
|---|---|
| Class | XmlToXDocumentPropertyHandler |
| Namespace | RepoDb.PropertyHandlers |
| Package | RepoDb |
| Implements | IPropertyHandler<string, XDocument> |
| Column type | The SQL Server xml type, returned by the database as a string |
| Property type | XDocument |
Conversions
| Method | Direction | Description |
|---|---|---|
| Get | Database to property | Parses the XML text via XDocument.Parse(). A null or an empty value is returned as null. |
| Set | Property to database | Converts the XDocument into its unformatted XML text via ToString(SaveOptions.DisableFormatting). A null value is written as null. |
Usability
Bind the handler to the property that maps to an xml column.
Attribute
public class Document
{
public int Id { get; set; }
[Map("content"), PropertyHandler(typeof(XmlToXDocumentPropertyHandler))]
public XDocument Content { get; set; }
}
Once bound, the library invokes the handler automatically when the property is read or written.
using (var connection = new SqlConnection(connectionString))
{
var content = XDocument.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<XmlToXDocumentPropertyHandler>(e => e.Content);
Property Level Mapping
To configure via PropertyHandlerMapper:
PropertyHandlerMapper.Add<Document, XmlToXDocumentPropertyHandler>(e => e.Content, new XmlToXDocumentPropertyHandler(), true);
Type Level Mapping
To apply the handler to every property of the type XDocument:
PropertyHandlerMapper.Add<XDocument, XmlToXDocumentPropertyHandler>(new XmlToXDocumentPropertyHandler(), true);