Saltar al contenido

Ejemplo de Script: Exportación a XML

Este ejemplo es un complemento de tipo Informe, diseñado para exportar y descargar el contenido de un panel de varias filas a un archivo XML. Para utilizar este complemento, debe registrarlo en una página de Informe en una aplicación.

Ejemplo de Secuencia de Comandos

using System;
using System.IO;
using System.Drawing;
using Vinyl.Sdk;
using System.Data;
using System.Text;
using System.Xml;
using System.Collections.Generic;



string debug = "";
bool isdebug = false; //0=Debug Off or 1=Debug On
bool exception = false;
string FileUUID = System.Guid.NewGuid().ToString();
string filename = "XMLExport.xml";


//REVIEWING PANEL DATA


        try
        {

            System.IO.Directory.CreateDirectory(@"C:\XMLGenerator\");

            //using (var stream = new FileStream(@"C:\XMLGenerator\XMLFile"+FileUUID+".xml", FileMode.Create, System.IO.FileAccess.Write));
            using XmlWriter x = XmlWriter.Create(@"C:\XMLGenerator\XMLFile"+FileUUID+".xml");



            var dataSet = await ReportInfo.GetReportAsDataSetAsync();
                foreach(DataTable table in dataSet.Tables)
                {
                  if (table.TableName != "User")
                  {


                        debug = debug + "\r\nTable: " + table.TableName;
                        foreach (DataColumn column in table.Columns)
                        {
                            debug = debug + "\r\nColumn: " + column.ColumnName;
                        }


                         x.WriteStartDocument();
                        var rowCount = table.Rows.Count;
                        var columnCount = table.Columns.Count;


                        x.WriteStartElement("Transactions");




                        //Set Header
                        List<string>names = new List<string>();
                        foreach (DataColumn column in table.Columns)
                            names.Add(column.ColumnName);

                        //Insert Data

                        for (var i = 0; i < rowCount; i++)
                        {
                            x.WriteStartElement("Transaction");
                            for (var j = 0; j < columnCount; j++){

                                x.WriteElementString(names[j],table.Rows[i][yo].ToString());
                                //x.WriteEndElement();
                                //if (table.Rows[i][yo].ToString() != null)
                                //debug = debug + "\r\n" + table.Rows[i][yo].ToString();

                            }
                            x.WriteEndElement();

                        }
                            x.WriteEndDocument();
                            x.Flush();

                  }
                  else {
                      filename = table.Rows[0][0].ToString();
                  }

                }
            }




        catch (Exception e)
        {
                        //DEBUG MODE
                        debug = debug + "\r\n\r\nERROR: " + e;
                        exception=true;

        }


        if (isdebug == false && exception == false)
        {
                    ReportInfo.ContentType ="xml";
                    ReportInfo.FileName = filename;
                    using (FileStream file = new FileStream(@"C:\XMLGenerator\XMLFile"+FileUUID+".xml", FileMode.Open, FileAccess.Read))
                    {
                        await file.CopyToAsync(ReportInfo.Content);
                    }

        }

        else //Force DEBUG
        {
                byte[] bytes = null;

                    using (var ms = new MemoryStream())
                     {
                         TextWriter tw = new StreamWriter(ms);
                         tw.Write(debug);
                         tw.Flush();
                         ms.Position = 0;
                         bytes = ms.ToArray();
                         ReportInfo.FileName = "Debug.txt";
                         ReportInfo.ContentType ="txt";

                        await ms.CopyToAsync(ReportInfo.Content);
                     }
        }