/// <summary>
        /// Renders this MatrixReport in RDL.
        /// </summary>
        /// <returns>A Byte array containing the raw RDL data.</returns>
        public Byte[] Render()
        {
            //TODO: Code Review Issue 11/05/05: Add necessary comment on each code block and
            //indent the code as in XML where possible to increase readability
            // Done 24/05/2005
            _xmlWriter.WriteStartDocument();

            _xmlWriter.WriteStartElement("Report");
            _xmlWriter.WriteAttributeString("xmlns",
                                            "http://schemas.microsoft.com/sqlserver/reporting/2003/10/reportdefinition");

            RdlRender.AddLine(_xmlWriter, "Width", "1in");
            RdlRender.AddLine(_xmlWriter, "PageWidth", "11.5in");
            RdlRender.AddLine(_xmlWriter, "TopMargin", "0.25in");
            RdlRender.AddLine(_xmlWriter, "BottomMargin", "0.25in");
            RdlRender.AddLine(_xmlWriter, "LeftMargin", "0.25in");
            RdlRender.AddLine(_xmlWriter, "RightMargin", "0.25in");
            RdlRender.AddLine(_xmlWriter, "PageHeight", "7.9375in");

            _xmlWriter.WriteStartElement("PageHeader");
            _xmlWriter.WriteStartElement("ReportItems");
            RdlRender.AddImage(_xmlWriter, "Image1", "2.59259cm", "3.8cm",
                               "1.0cm", "Embedded", "Esf", "AutoSize");
            RdlRender.AddTextbox(_xmlWriter, "TitleTextbox", "DWP Small Systems Ad Hoc Reporting System",
                                 _textboxStyle, "Left", "0.3cm", "5.0cm", "10cm", "true", "0.95cm", "2", "",
                                 "", "", "", "", "", "", "");
            RdlRender.AddTextbox(_xmlWriter, "PageHeader", "=Globals!ReportName",
                                 _textboxStyle, "Left", "2.85714cm", "1.26984cm", "14.92063cm", "true",
                                 "", "", "", "", "", "", "", "", "", "");


            foreach (String _parameterName in _parameterList)
            {
                _parameterCount++;

                RdlRender.AddTextbox(_xmlWriter, "Parameter" + _parameterCount.ToString(), _parameterName, _textboxStyle,
                                     "Left", "2.85714cm", "1.26984cm", "14.92063cm", "true", "", "", "", "",
                                     "", "", "", "", "", "");
            }
            _xmlWriter.WriteEndElement();                     // ReportItems
            RdlRender.AddLine(_xmlWriter, "PrintOnLastPage", "true");
            RdlRender.AddLine(_xmlWriter, "PrintOnFirstPage", "true");
            RdlRender.AddLine(_xmlWriter, "Height", "1.375in");
            _xmlWriter.WriteEndElement();                     // PageHeader

            _xmlWriter.WriteStartElement("Body");

            RdlRender.AddLine(_xmlWriter, "Height", "5in");
            _xmlWriter.WriteStartElement("ReportItems");

            // Add table of keys to dynamic rows/columns
            ReportingServicesStyle BoldBorderedTextBoxStyle = new ReportingServicesStyle(ReportingServicesStyle.TextBoxStyle.BoldBordered);
//						"", "Solid", "Black", "", "", "", "", "10pt", "Bold", "", "", "", "");
            ReportingServicesStyle PlainBorderedTextBoxStyle = new ReportingServicesStyle(ReportingServicesStyle.TextBoxStyle.PlainBordered);

//						"", "Solid", "Black", "", "", "", "", "10pt", "", "", "", "", "");

            // Table's header
            _xmlWriter.WriteStartElement("Table");
            _xmlWriter.WriteAttributeString("Name", "KeyTable");
            RdlRender.AddLine(_xmlWriter, "Left", "1.26984cm");

            RdlRender.AddLine(_xmlWriter, "DataSetName", _matrixDataSetName + "DataSet");
            _xmlWriter.WriteStartElement("Style");
            _xmlWriter.WriteStartElement("BorderStyle");
            RdlRender.AddLine(_xmlWriter, "Default", PlainBorderedTextBoxStyle.BorderStyle);
            _xmlWriter.WriteEndElement();                                     // BorderStyle
            _xmlWriter.WriteEndElement();                                     // Style
            _xmlWriter.WriteStartElement("Header");
            _xmlWriter.WriteStartElement("TableRows");
            _xmlWriter.WriteStartElement("TableRow");
            RdlRender.AddLine(_xmlWriter, "Height", "1cm");
            _xmlWriter.WriteStartElement("TableCells");
            _xmlWriter.WriteStartElement("TableCell");
            _xmlWriter.WriteStartElement("ReportItems");
            RdlRender.AddTextbox(_xmlWriter, "KeyTableRows", "Dynamic Rows",
                                 BoldBorderedTextBoxStyle, "Center", "", "", "", "", "", "", "", "", "",
                                 "", "", "", "", "");
            _xmlWriter.WriteEndElement();                                                                     // ReportItems
            _xmlWriter.WriteEndElement();                                                                     // TableCell
            _xmlWriter.WriteStartElement("TableCell");
            _xmlWriter.WriteStartElement("ReportItems");
            RdlRender.AddTextbox(_xmlWriter, "KeyTableColumns", "Dynamic Columns",
                                 BoldBorderedTextBoxStyle, "Center", "", "", "", "", "", "", "", "", "",
                                 "", "", "", "", "");
            _xmlWriter.WriteEndElement();                                             // ReportItems
            _xmlWriter.WriteEndElement();                                             // TableCell
            _xmlWriter.WriteEndElement();                                             // TableCells
            _xmlWriter.WriteEndElement();                                             // TableRow

            // Add the names of the rows and columns to the table
            for (int RowNum = 0;
                 RowNum < _columnTotalDataItemList.Count && RowNum < _rowTotalDataItemList.Count;
                 RowNum++)
            {
                string ColumnItem = _columnTotalDataItemList[RowNum] != null ?
                                    (_columnTotalDataItemList[RowNum]).Caption : "";
                string RowItem = _rowTotalDataItemList[RowNum] != null ?
                                 (_rowTotalDataItemList[RowNum]).Caption : "";

                _xmlWriter.WriteStartElement("TableRow");
                RdlRender.AddLine(_xmlWriter, "Height", "0.8cm");
                _xmlWriter.WriteStartElement("TableCells");
                _xmlWriter.WriteStartElement("TableCell");
                _xmlWriter.WriteStartElement("ReportItems");

                // Column text box
                RdlRender.AddTextbox(_xmlWriter, "KeyTableRow" + RowNum, RowItem,
                                     PlainBorderedTextBoxStyle, "", "", "", "", "", "", "", "", "", "", "",
                                     "", "", "", "");

                _xmlWriter.WriteEndElement();                                                                 // ReportItems
                _xmlWriter.WriteEndElement();                                                                 // TableCell
                _xmlWriter.WriteStartElement("TableCell");
                _xmlWriter.WriteStartElement("ReportItems");

                // Row text box
                RdlRender.AddTextbox(_xmlWriter, "KeyTableColumn" + RowNum, ColumnItem,
                                     PlainBorderedTextBoxStyle, "", "", "", "", "", "", "", "", "", "", "",
                                     "", "", "", "");

                _xmlWriter.WriteEndElement();                                                 // ReportItems
                _xmlWriter.WriteEndElement();                                                 // TableCell
                _xmlWriter.WriteEndElement();                                                 // TableCells
                _xmlWriter.WriteEndElement();                                                 // TableRow
            }

            _xmlWriter.WriteEndElement();                                     // TableRows
            _xmlWriter.WriteEndElement();                                     // Header
            _xmlWriter.WriteStartElement("TableColumns");
            _xmlWriter.WriteStartElement("TableColumn");
            RdlRender.AddLine(_xmlWriter, "Width", "5.0cm");
            _xmlWriter.WriteEndElement();                                             // TableColumn
            _xmlWriter.WriteStartElement("TableColumn");
            RdlRender.AddLine(_xmlWriter, "Width", "5.0cm");
            _xmlWriter.WriteEndElement();                             // TableColumn
            _xmlWriter.WriteEndElement();                             // TableColumns
            _xmlWriter.WriteEndElement();                             // Table

            // End of table of keys rendering


            _xmlWriter.WriteStartElement("Matrix");
            _xmlWriter.WriteAttributeString("Name", "Matrix1");
            RdlRender.AddLine(_xmlWriter, "DataSetName", _matrixDataSetName + "DataSet");
            RdlRender.AddLine(_xmlWriter, "Top", "2.5cm");
            RdlRender.AddLine(_xmlWriter, "Left", ".5in");
            RdlRender.AddLine(_xmlWriter, "Height", ".5in");
            RdlRender.AddLine(_xmlWriter, "Width", "6in");

            _xmlWriter.WriteStartElement("Corner");
            _xmlWriter.WriteStartElement("ReportItems");

            RdlRender.AddTextbox(_xmlWriter, "CornerTextbox", "", _cornerTextboxStyle,
                                 "", "", "", "", "true", "", "", "", "", "", "", "", "", "", "");

            _xmlWriter.WriteEndElement();                                     // ReportItems
            _xmlWriter.WriteEndElement();                                     // Corner


            // Render MatrixRows
            _xmlWriter.WriteStartElement("MatrixRows");
            foreach (MatrixRow CurrentRow in _matrixRowList)
            {
                CurrentRow.Render(_xmlWriter);
            }
            _xmlWriter.WriteEndElement();                                     // MatrixRows

            _xmlWriter.WriteStartElement("MatrixColumns");
            _xmlWriter.WriteStartElement("MatrixColumn");
            RdlRender.AddLine(_xmlWriter, "Width", "3.25cm");
            _xmlWriter.WriteEndElement();                             // MatrixColumns
            _xmlWriter.WriteEndElement();                             // MatrixColumn


            // Render ColumnGroupings
            _xmlWriter.WriteStartElement("ColumnGroupings");
            foreach (MatrixDynamicColumnGrouping CurrentColGroup in _dynamicColumnGroupingList)
            {
                CurrentColGroup.Render(_xmlWriter);
            }
            _xmlWriter.WriteEndElement();


            // Render RowGroupings
            _xmlWriter.WriteStartElement("RowGroupings");
            foreach (MatrixDynamicRowGrouping CurrentRowGroup in _dynamicRowGroupingList)
            {
                CurrentRowGroup.Render(_xmlWriter);
            }

            _xmlWriter.WriteStartElement("RowGrouping");
            RdlRender.AddLine(_xmlWriter, "Width", "3.5cm");
            _xmlWriter.WriteStartElement("StaticRows");
            foreach (MatrixStaticRow CurrentRow in _matrixStaticRowList)
            {
                CurrentRow.Render(_xmlWriter);
            }
            _xmlWriter.WriteEndElement();                     // StaticRows
            _xmlWriter.WriteEndElement();                     // RowGrouping
            _xmlWriter.WriteEndElement();                     // RowGroupings
            RdlRender.AddLine(_xmlWriter, "NoRows", Resource.GetString("RES_REPORT_NOROWSRETURNED"));
            _xmlWriter.WriteEndElement();                     // Matrix
            _xmlWriter.WriteEndElement();                     // ReportItems

            RdlRender.AddLine(_xmlWriter, "ColumnSpacing", "1cm");

            _xmlWriter.WriteEndElement();                     // Body


            // DataSources
            _xmlWriter.WriteStartElement("DataSources");
            _xmlWriter.WriteStartElement("DataSource");
            _xmlWriter.WriteAttributeString("Name", "DataSource1");
            //		_xmlWriter.WriteStartElement("ConnectionProperties");
            RdlRender.AddLine(_xmlWriter, "DataSourceReference", "/" + _dataSource);

            //					RdlRender.AddLine(_xmlWriter, "DataProvider", "SQL");
            //					RdlRender.AddLine(_xmlWriter, "ConnectString", _dbConnectionString);
            //					RdlRender.AddLine(_xmlWriter, "IntegratedSecurity", "false");
            //					RdlRender.AddLine(_xmlWriter, "Prompt", "false");

            //				_xmlWriter.WriteEndElement(); // ConnectionProperties
            _xmlWriter.WriteEndElement();                     // DataSource
            _xmlWriter.WriteEndElement();                     // DataSources


            // Datasets
            _xmlWriter.WriteStartElement("DataSets");
            foreach (Dataset CurrentDataset in _datasetList)
            {
                CurrentDataset.Render(_xmlWriter);
            }
            _xmlWriter.WriteEndElement();

            // ReportParameters
            if (_reportParameterList != null && _reportParameterList.Count > 0)
            {
                _xmlWriter.WriteStartElement("ReportParameters");
                foreach (ReportParameter CurrentParameter in _reportParameterList)
                {
                    CurrentParameter.Render(_xmlWriter);
                }
                _xmlWriter.WriteEndElement();
            }

            // EmbeddedImages
            _xmlWriter.WriteStartElement("EmbeddedImages");
            _xmlWriter.WriteStartElement("EmbeddedImage");
            _xmlWriter.WriteAttributeString("Name", "Esf");
            RdlRender.AddLine(_xmlWriter, "MIMEType", "image/bmp");
            _xmlWriter.WriteStartElement("ImageData");

            // Image data
            FileStream ImageStream = new FileStream(HttpContext.Current.Server.MapPath(
                                                        ConfigurationManager.AppSettings["SmallSystemsLogoImage"]), FileMode.Open, FileAccess.Read);

            byte[] ImageByteArray = new Byte[ImageStream.Length];
            ImageStream.Read(ImageByteArray, 0, (int)ImageStream.Length);
            ImageStream.Close();
            _xmlWriter.WriteBase64(ImageByteArray, 0, ImageByteArray.Length);

            _xmlWriter.WriteEndElement();                     // ImageData
            _xmlWriter.WriteEndElement();                     // EmbeddedImage
            _xmlWriter.WriteEndElement();                     // EmbeddedImages


            // Report footer
            _xmlWriter.WriteStartElement("PageFooter");
            RdlRender.AddLine(_xmlWriter, "Height", "1.0cm");
            RdlRender.AddLine(_xmlWriter, "PrintOnFirstPage", "true");
            RdlRender.AddLine(_xmlWriter, "PrintOnLastPage", "true");
            _xmlWriter.WriteStartElement("ReportItems");

            RdlRender.AddTextbox(_xmlWriter, "ReportDescriptionTextBox", _reportDescription,
                                 BoldBorderedTextBoxStyle, "", "0.25cm", "1.0cm", "16.00cm", "true", "0.5cm", "1",
                                 "", "", "", "", "", "", "", "");


            //Report Filters

            foreach (ReportFilterBusinessObject _CurrentFilter in _filterItemList)
            {
                FilterCount++;
                _Filter = "Filter" + FilterCount.ToString();

                _FilterDescription = "Data Filter Applied : " + _CurrentFilter.DataItemCaption + " " + _CurrentFilter.Operand + " " + _CurrentFilter.FilterValue;
                RdlRender.AddTextbox(_xmlWriter, _Filter, _FilterDescription, BoldBorderedTextBoxStyle, "", "0.25cm", "1.00cm", "16.00cm", "true", "0.5cm", "1",
                                     "", "", "", "", "", "", "", "");
            }


            _xmlWriter.WriteEndElement();             // Report
            _xmlWriter.WriteEndDocument();
            _xmlWriter.Flush();

            _memoryStream.Close();
            return(_memoryStream.GetBuffer());

            //_stream.Close();
            //return new byte[0];
        }