Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

XML Serializing dapper results

I store SQL results into a dynamic List, of which has an underlying DapperRow type. I am trying to serialize/unserialize this List of which XMLserializer complains:

There was an error generating the XML document. ---> System.InvalidOperationException: To be XML serializable, types which inherit from IEnumerable must have an implementation of Add(System.Object) at all levels of their inheritance hierarchy. Dapper.SqlMapper+DapperRow does not implement Add(System.Object).

Is there a way around this (besides the obvious casting the results to my own concrete object), or is it possible to somehow make DapperRow objects conform to System.Xml.XMLserializer constraints?

It states my result array is System.Collections.Generic.List<dynamic> {System.Collections.Generic.List<object>} Opening the array it says each object is of type object {Dapper.SqlMapper.DapperRow}

I think because DapperRows are now basically IDictionary<string, object> that XML is having issues (I cannot use anything but System.Xml.XmlSerializer so don't suggest an alternative).

I just want to be able to turn a List<dynamic> that I get from Dapper and serialize and deserialize correctly using System.Xml.XmlSerializer

like image 243
ParoX Avatar asked Nov 09 '22 03:11

ParoX


1 Answers

I was able to get something at least serializable by using a serializable dictionary : http://weblogs.asp.net/pwelter34/444961

            var results = conn.Query<dynamic>(sql, param);
            var resultSet = new List<SerializableDictionary<string, object>>();
            foreach (IDictionary<string, object> row in results)
            {
                var dict = new SerializableDictionary<string, object>();
                foreach (var pair in row)
                {
                    dict.Add(pair.Key, pair.Value);
                }
                resultSet.Add(dict);
            }

Its ugly, so I hope more elegant solutions come up

like image 146
ParoX Avatar answered Nov 14 '22 21:11

ParoX