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
        } // GetAssemblyQualifiedNoVersionName

        private static string GetTypeName(System.Type type, XmlRenderType_t renderType)
        {
            if (type == null)
            {
                return(null);
            }

            if (renderType.HasFlag(XmlRenderType_t.AssemblyQualifiedName))
            {
                return(GetAssemblyQualifiedNoVersionName(type));
            }

            if (renderType.HasFlag(XmlRenderType_t.LongName))
            {
                return(type.FullName);
            }

            return(type.Name);
        } // GetAssemblyQualifiedNoVersionName
Example #3
0
        private static System.Xml.XmlWriter CreateXmlWriter(System.Text.StringBuilder builder, System.IO.TextWriter writer, System.Text.Encoding encoding, XmlRenderType_t renderType)
        {
            System.Xml.XmlWriterSettings xs = new System.Xml.XmlWriterSettings();

            if (renderType.HasFlag(XmlRenderType_t.Indented))
            {
                xs.Indent = true;
            }
            else
            {
                xs.Indent = false;
            }

            xs.Async              = true;
            xs.IndentChars        = "    ";
            xs.NewLineChars       = System.Environment.NewLine;
            xs.OmitXmlDeclaration = false; // // <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
            // xs.Encoding = System.Text.Encoding.UTF8; // doesn't work with pgsql
            // xs.Encoding = new System.Text.UTF8Encoding(false);
            xs.Encoding    = encoding;
            xs.CloseOutput = true;

            // string exportFilename = System.IO.Path.Combine(@"d:\", table_name + ".xml");
            // using (System.Xml.XmlWriter writer = System.Xml.XmlWriter.Create(exportFilename, xs))

            if (writer != null)
            {
                return(System.Xml.XmlWriter.Create(writer, xs));
            }

            StringWriterWithEncoding sw = new StringWriterWithEncoding(builder, xs.Encoding);

            return(System.Xml.XmlWriter.Create(sw, xs));
        } // End Function CreateXmlWriter
Example #4
0
 private static System.Xml.XmlWriter CreateXmlWriter(Microsoft.AspNetCore.Http.HttpContext context, System.Text.Encoding encoding, XmlRenderType_t renderType)
 {
     return(CreateXmlWriter(context.Response.Body, encoding, renderType));
 }
Example #5
0
        private static System.Xml.XmlWriter CreateXmlWriter(System.IO.Stream stream, System.Text.Encoding encoding, XmlRenderType_t renderType)
        {
            System.IO.TextWriter tw = new System.IO.StreamWriter(stream, encoding);

            return(CreateXmlWriter(null, tw, encoding, renderType));
        }
Example #6
0
 private static System.Xml.XmlWriter CreateXmlWriter(System.IO.StreamWriter writer, XmlRenderType_t renderType)
 {
     return(CreateXmlWriter(null, writer, writer.Encoding, renderType));
 }
Example #7
0
 private static System.Xml.XmlWriter CreateXmlWriter(System.Text.StringBuilder builder, System.Text.Encoding encoding, XmlRenderType_t renderType)
 {
     return(CreateXmlWriter(builder, null, encoding, renderType));
 }
Example #8
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 #9
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