Exemplo n.º 1
0
        /// <summary>
        /// Export a list of logs to an XML file
        /// </summary>
        /// <param name="logs">The list of logs that needs to be exported</param>
        public async void Export(List <Log> logs)
        {
            // Create a new file, if duplicate, create unique name
            StorageFile currentFile = await CreateFile("Orbis Log", "xml", CreationCollisionOption.GenerateUniqueName);

            using (IRandomAccessStream writeStream = await currentFile.OpenAsync(FileAccessMode.ReadWrite))
            {
                // Create an output stream to write to
                Stream s = writeStream.AsStreamForWrite();

                // Create settings to write Async and use indent in file
                System.Xml.XmlWriterSettings settings = new System.Xml.XmlWriterSettings
                {
                    Async  = true,
                    Indent = true,
                };

                // Create the actual writer with defined settings to write to file
                using (System.Xml.XmlWriter writer = System.Xml.XmlWriter.Create(s, settings))
                {
                    writer.WriteStartElement("Logs");
                    string timestamp = DateTime.Now.ToString();

                    // Write each log in a new Log node
                    foreach (Log log in logs.ToArray())
                    {
                        writer.WriteStartElement("Log");
                        writer.WriteAttributeString("exportedAt", timestamp);   // Timestamp of when the log got exported

                        // Write al log data dynamically
                        foreach (KeyValuePair <string, string> data in log.GetData())
                        {
                            writer.WriteElementString(data.Key, data.Value);
                        }
                        writer.WriteEndElement();
                    }
                    await writer.FlushAsync();
                }
            }
        }
Exemplo n.º 2
0
 public static async Task WriteXmlFile(Windows.Storage.StorageFile file, Person person)
 {
     try
     {
         using (IRandomAccessStream writeStream = await file.OpenAsync(FileAccessMode.ReadWrite))
         {
             System.IO.Stream             s        = writeStream.AsStreamForWrite();
             System.Xml.XmlWriterSettings settings = new System.Xml.XmlWriterSettings();
             settings.Async = true;
             using (System.Xml.XmlWriter writer = System.Xml.XmlWriter.Create(s, settings))
             {
                 writer.WriteStartElement("Person");
                 writer.WriteElementString("Name", person.Name);
                 writer.WriteElementString("Age", Convert.ToString(person.Age));
                 writer.WriteElementString("City", person.City);
                 writer.Flush();
                 await writer.FlushAsync();
             }
         }
     }
     catch { }
 }
Exemplo n.º 3
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
Exemplo n.º 4
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