Skip to Content

Scripting Example - Export to XML

This example is a Plugin type of Report, designed to export and download the contents of a Multi-row Panel to an XML file. To use this Plugin, you must register it on a Report page in an app.

Example Script

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][j].ToString());
                                //x.WriteEndElement();
                                //if (table.Rows[i][j].ToString() != null)
                                //debug = debug + "\r\n" + table.Rows[i][j].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);
                     }
        }