2

We have recently updated our solution from Visual Studio 2010 to VS 2017 & .NET Framework 4.0 to 4.6.1.

There are no changes in database which is running on Postgres 9.4.

This also included upgrading Npgsql DLL from version 2 to version 3.

We have a piece of code like below:

using (MemoryStream memoryStream = new MemoryStream()) { dataTable.WriteXml(memoryStream, XmlWriteMode.IgnoreSchema); . . .

We have a PostgreSQL stored procedure which is returning columns of array data types (string[], bool[], int[] etc.).

In .NET 4.0, NpgSql 2 version above line was working fine with no issues. But with new setup, we are getting a serialization error.

Type 'System.String[], mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089' does not implement IXmlSerializable interface therefore can not proceed with serialization. at System.Data.XmlDataTreeWriter.XmlDataRowWriter(DataRow row, String encodedTableName)
at System.Data.XmlDataTreeWriter.Save(XmlWriter xw, Boolean writeSchema)
at System.Data.DataTable.WriteXml(XmlWriter writer, XmlWriteMode mode, Boolean writeHierarchy)
at System.Data.DataTable.WriteXml(Stream stream, XmlWriteMode mode, Boolean writeHierarchy)

The main thing I could notice in VS debugger was that the DataType of those array columns are shown as "System.Array" from VS 2017 but before (in 2010) it was showing specific type array like "System.String[]".

Anyone knows what is going on here? Any help/suggestion is highly appreciated.

Here is a simple code that can be used to replicate the issue.

using System.Configuration;
using Npgsql;
using System.Data;
using System.IO;

namespace NpgSqlArrayTest
{
    public class Program
    {
        public static void Main(string[] args)
        {
            string connectionString = ConfigurationManager.ConnectionStrings["TestConnection"].ConnectionString;
            NpgsqlConnection npgsqlConnection = new NpgsqlConnection(connectionString);
            DataTable resultTable = new DataTable();
            NpgsqlDataAdapter npgsqlDataAdapter = new NpgsqlDataAdapter("select * from \"FunctionReturnsArray\"();", npgsqlConnection);
            npgsqlConnection.Open();
            npgsqlDataAdapter.Fill(resultTable);
            resultTable.TableName = "FunctionReturnsArray";
            npgsqlConnection.Close();

            using (MemoryStream memoryStream = new MemoryStream())
            {
                resultTable.WriteXml(memoryStream, XmlWriteMode.IgnoreSchema);
            }
        }
    }
}

This is the stored procedure.

CREATE OR REPLACE FUNCTION "FunctionReturnsArray"()
RETURNS integer[] AS
$BODY$
declare
    v_Count integer;
    v_Array integer[];
begin
    v_Count = 0;
    while v_Count < 10 loop
        v_Array = array_append(v_Array, v_Count);
        v_Count = v_Count + 1;
    end loop;
    return v_Array;
end;
$BODY$
  LANGUAGE plpgsql STABLE
  COST 100;
Arun
  • 2,217
  • 3
  • 17
  • 18
  • Missed to mention that the "dataTable" object is filled by results from Postgres procedure. – Arun Apr 25 '18 at 13:41
  • 2
    This is likely a result of the change from Npgsql 2 to 3, rather than a .NET Framework change. Can you please isolate this in a minimal code sample and submit an issue with it on the [github site](http://github.com/npgsql/npgsql)? – Shay Rojansky Apr 25 '18 at 14:00
  • Found a workaround. Cloned the dataTable, updated the DataType of array columns to type specific ones (i.e. System.Array to System.String[] or Int[]) and imported data from data table to the cloned ones. After that if I try the above code with cloned data table, it works. – Arun Apr 25 '18 at 14:03
  • 1
    Neither `System.Array` or `System.String[]` implement `IXmlSerializable`. Both types are serializable when you check `Type.IsSerializable`. Agree with @ShayRojansky - not a .NET problem. – P.Brian.Mackey Apr 25 '18 at 14:45
  • @ShayRojansky, please see above a sample code that can reproduce this error. – Arun May 10 '18 at 08:08
  • Thanks for the repro, although I admit I don't know much about how `DataTable.WriteXml()` works and won't have time in the near future to dive into this... If someone else has some knowledge and wants to take a look that would be great. – Shay Rojansky May 10 '18 at 15:05

0 Answers0