Example #1
0
        } // GetAssemblyQualifiedNoVersionName

        private static async System.Threading.Tasks.Task WriteColumnDefinition(
            System.Xml.XmlWriter writer
            , System.Data.IDataReader dr
            , XmlRenderType_t renderType)
        {
            await writer.WriteStartElementAsync(null, "columns", null);

            for (int i = 0; i <= dr.FieldCount - 1; i++)
            {
                await writer.WriteStartElementAsync(null, "column", null);

                await writer.WriteAttributeStringAsync(null, "name", null, dr.GetName(i));

                await writer.WriteAttributeStringAsync(null, "index", null, i.ToString(System.Globalization.CultureInfo.InvariantCulture));

                if (renderType.HasFlag(XmlRenderType_t.WithDetail))
                {
                    await writer.WriteAttributeStringAsync(null, "fieldType", null, GetTypeName(dr.GetFieldType(i), renderType));
                }

                await writer.WriteEndElementAsync(); // column
            }

            await writer.WriteEndElementAsync(); // columns
        } // WriteColumnDefinition
Example #2
0
        } // WriteColumnDefinition

        private static async System.Threading.Tasks.Task WriteAsXmlAsync(
            string table_schema
            , string table_name
            , XmlRenderType_t format
            , System.Xml.XmlWriter writer
            , System.Data.IDataReader dr)
        {
            await writer.WriteStartDocumentAsync(true);

            bool dataAsAttributes = format.HasFlag(XmlRenderType_t.DataInAttributes);


            bool dataTableOnly = format.HasFlag(XmlRenderType_t.DataTable);

            if (!dataTableOnly)
            {
                await writer.WriteStartElementAsync(null, "dataset", null);
            }

            do
            {
                await writer.WriteStartElementAsync(null, "table", null);

                if (dataTableOnly)
                {
                    if (table_schema != null)
                    {
                        await writer.WriteAttributeStringAsync(null, "table_schema", null, table_schema);
                    }

                    if (table_name != null)
                    {
                        await writer.WriteAttributeStringAsync(null, "table_name", null, table_name);
                    }
                }

                if (!dataAsAttributes)
                {
                    await writer.WriteAttributeStringAsync("xmlns", "xsi", null, System.Xml.Schema.XmlSchema.InstanceNamespace);
                }

                int fc = dr.FieldCount;

                if (format.HasFlag(XmlRenderType_t.WithColumnDefinition))
                {
                    await WriteColumnDefinition(writer, dr, format);
                }

                string[]      columnNames = new string[fc];
                System.Type[] columnTypes = new System.Type[fc];

                for (int i = 0; i < dr.FieldCount; ++i)
                {
                    columnNames[i] = dr.GetName(i);
                    columnTypes[i] = dr.GetFieldType(i);
                } // Next i


                while (dr.Read())
                {
                    await writer.WriteStartElementAsync(null, "row", null);

                    for (int i = 0; i < fc; ++i)
                    {
                        if (!dataAsAttributes)
                        {
                            await writer.WriteStartElementAsync(null, columnNames[i], null);
                        }

                        object obj = dr.GetValue(i);

                        if (obj != System.DBNull.Value)
                        {
                            string value = null;

                            if (object.ReferenceEquals(columnTypes[i], typeof(System.DateTime)))
                            {
                                System.DateTime dt = (System.DateTime)obj;
                                value = dt.ToString("yyyy-MM-dd'T'HH':'mm':'ss'.'fff", System.Globalization.CultureInfo.InvariantCulture);
                            }
                            else
                            {
                                value = System.Convert.ToString(obj, System.Globalization.CultureInfo.InvariantCulture);
                            }

                            if (dataAsAttributes)
                            {
                                await writer.WriteAttributeStringAsync(null, columnNames[i], null, value);
                            }
                            else
                            {
                                writer.WriteValue(value);
                            }
                        } // End if (obj != System.DBNull.Value)
                        else
                        {
                            if (!dataAsAttributes)
                            {
                                await writer.WriteAttributeStringAsync("xsi", "nil", System.Xml.Schema.XmlSchema.InstanceNamespace, "true");
                            }
                        }

                        if (!dataAsAttributes)
                        {
                            await writer.WriteEndElementAsync(); // column
                        }
                    } // Next i

                    await writer.WriteEndElementAsync(); // row
                } // Whend

                await writer.WriteEndElementAsync(); // table

                await writer.FlushAsync();

                if (dataTableOnly)
                {
                    break;
                }
            } while (dr.NextResult());

            if (!dataTableOnly)
            {
                await writer.WriteEndElementAsync(); // dataset

                await writer.FlushAsync();
            }
        } // End Sub WriteAsXmlAsync
Example #3
0
        public static async System.Threading.Tasks.Task AnyDataReaderToXml(this System.Data.IDbConnection cnn
                                                                           , string sql
                                                                           , object param                  = null
                                                                           , string tableSchema            = null
                                                                           , string tableName              = null
                                                                           , System.Text.Encoding encoding = null
                                                                           , XmlRenderType_t format        = XmlRenderType_t.Default
                                                                           , Microsoft.AspNetCore.Http.HttpContext context = null
                                                                           , System.Data.IDbTransaction transaction        = null
                                                                           , int?commandTimeout = null
                                                                           , System.Data.CommandType?commandType = null)
        {
            if (encoding == null)
            {
                encoding = System.Text.Encoding.UTF8;
            }

            if (string.IsNullOrEmpty(sql))
            {
                if (string.IsNullOrEmpty(tableName))
                {
                    throw new System.ArgumentException("Parameter " + nameof(tableName) + " is NULL or empty.");
                }

                if (string.IsNullOrEmpty(tableSchema))
                {
                    sql = $"SELECT * FROM " + QuoteObject(tableName) + " ;";
                }
                else
                {
                    sql = $"SELECT * FROM " + QuoteObject(tableSchema) + "." + QuoteObject(tableName) + " ;";
                }
            } // End if (string.IsNullOrEmpty(sql))

            if (string.IsNullOrEmpty(sql))
            {
                throw new System.ArgumentException("Parameter " + nameof(sql) + " is NULL or empty.");
            }

            // DynamicParameters dbArgs = new DynamicParameters();

            System.Text.StringBuilder xmlBuilder = new System.Text.StringBuilder();

            using (System.IO.StreamWriter output =
                       new System.IO.StreamWriter(context.Response.Body, encoding))
            {
                // using (System.Xml.XmlWriter writer = CreateXmlWriter(xmlBuilder, format))
                using (System.Xml.XmlWriter writer = CreateXmlWriter(output, format))
                {
                    try
                    {
                        using (System.Data.Common.DbDataReader dr = await cnn.ExecuteDbReaderAsync(sql, param, transaction, commandTimeout, commandType))
                        {
                            if (context != null)
                            {
                                context.Response.StatusCode  = (int)System.Net.HttpStatusCode.OK;
                                context.Response.ContentType = "application/xml; charset=" + encoding.WebName;
                                // context.Response.Headers["Transfer-Encoding"] = "chunked";
                            } // End if (context != null)

                            await WriteAsXmlAsync(tableSchema, tableName, format, writer, dr);
                        } // End Using dr
                    }
                    catch (System.Exception ex)
                    {
                        context.Response.StatusCode  = (int)System.Net.HttpStatusCode.InternalServerError;
                        context.Response.ContentType = "application/xml; charset=" + encoding.WebName;

                        bool dataAsAttributes = format.HasFlag(XmlRenderType_t.DataInAttributes);


                        await writer.WriteStartDocumentAsync(true);

                        await writer.WriteStartElementAsync(null, "error", null);

                        if (dataAsAttributes)
                        {
                            await writer.WriteAttributeStringAsync(null, "Message", null, ex.Message);
                        }
                        else
                        {
                            await writer.WriteStartElementAsync(null, "Message", null);

                            writer.WriteValue(ex.Message);
                            await writer.WriteEndElementAsync(); // Message
                        }

                        if (dataAsAttributes)
                        {
                            await writer.WriteAttributeStringAsync(null, "StackTrace", null, ex.StackTrace);
                        }
                        else
                        {
                            await writer.WriteStartElementAsync(null, "StackTrace", null);

                            writer.WriteValue(ex.StackTrace);
                            await writer.WriteEndElementAsync(); // StackTrace
                        }

                        await writer.WriteEndElementAsync(); // error
                    }

                    await writer.FlushAsync();

                    await output.FlushAsync();

                    await context.Response.CompleteAsync();
                } // End Using writer
            }     // Wnd Using output
        }         // End Task AnyDataReaderToXml