@@ -177,6 +177,30 @@ public NpgsqlDriver(
177177 readerFn : ordinal => $ "reader.GetBoolean({ ordinal } )",
178178 readerArrayFn : ordinal => $ "reader.GetFieldValue<bool[]>({ ordinal } )"
179179 ) ,
180+ [ "XmlDocument" ] = new (
181+ new ( )
182+ {
183+ { "xml" , new ( NpgsqlTypeOverride : "NpgsqlDbType.Xml" ) }
184+ } ,
185+ readerFn : ordinal => $$ """
186+ (new Func<NpgsqlDataReader, int, XmlDocument>((r, o) =>
187+ {
188+ var xmlDoc = new XmlDocument();
189+ xmlDoc.LoadXml(r.GetString(o));
190+ return xmlDoc;
191+ }))({{ Variable . Reader . AsVarName ( ) }} , {{ ordinal }} )
192+ """ ,
193+ writerFn : ( el , notNull , isDapper ) =>
194+ {
195+ if ( notNull )
196+ return $ "{ el } .OuterXml";
197+ var nullValue = isDapper ? "null" : "(object)DBNull.Value" ;
198+ return $ "{ el } != null ? { el } .OuterXml : { nullValue } ";
199+ } ,
200+ usingDirective : "System.Xml" ,
201+ sqlMapper : "SqlMapper.AddTypeHandler(typeof(XmlDocument), new XmlDocumentTypeHandler());" ,
202+ sqlMapperImpl : XmlDocumentTypeHandler
203+ ) ,
180204 [ "NpgsqlPoint" ] = new (
181205 new ( )
182206 {
@@ -288,6 +312,28 @@ public NpgsqlDriver(
288312
289313 public override string TransactionClassName => "NpgsqlTransaction" ;
290314
315+ protected const string XmlDocumentTypeHandler =
316+ """
317+ private class XmlDocumentTypeHandler : SqlMapper.TypeHandler<XmlDocument>
318+ {
319+ public override XmlDocument Parse(object value)
320+ {
321+ if (value is string s)
322+ {
323+ var xmlDoc = new XmlDocument();
324+ xmlDoc.LoadXml(s);
325+ return xmlDoc;
326+ }
327+ throw new DataException($"Cannot convert {value?.GetType()} to XmlDocument");
328+ }
329+
330+ public override void SetValue(IDbDataParameter parameter, XmlDocument value)
331+ {
332+ parameter.Value = value.OuterXml;
333+ }
334+ }
335+ """ ;
336+
291337 public override ISet < string > GetUsingDirectivesForQueries ( )
292338 {
293339 return base . GetUsingDirectivesForQueries ( ) . AddRangeExcludeNulls (
0 commit comments