public void ReadingStructures(String structureFile, SdmxSchemaEnumType schema, String output){

            // Step 2. Implementing a helper class that parses DSD, Dataflow and writes datasets.
            //1)	Call the method getXmlWriter
            XmlWriter xmlWriter = this.getXmlWriter(output);

            //2)	Obtain IDataWriterEngine
            IDataWriterEngine compactWriter = new CompactDataWriterEngine(xmlWriter,SdmxSchema.GetFromEnum(schema));

            //3)	Read a structure file from a stream
            this.spm = new StructureParsingManager();
            ISdmxObjects structureObjects = new SdmxObjectsImpl();

            using (FileStream stream = File.Open(structureFile, FileMode.Open, FileAccess.Read))
            {
                using (IReadableDataLocation rdl = new ReadableDataLocationTmp(stream))
                {
                    IStructureWorkspace structureWorkspace = this.spm.ParseStructures(rdl);
                    structureObjects = structureWorkspace.GetStructureObjects(false);
                }
            }

            //4)	The IMaintainableRefObject is used to reference structures that extend the IMaintainableObject Interface.  
            IMaintainableRefObject dsdRef = new MaintainableRefObjectImpl("ESTAT", "STS", "2.2");
            IMaintainableRefObject flowRef = new MaintainableRefObjectImpl("ESTAT", "SSTSCONS_PROD_A", "1.0");

            //5)	Get the DataStructure and the Dataflow
            ISet<IDataStructureObject> dsd = structureObjects.GetDataStructures(dsdRef);
            ISet<IDataflowObject> dataflow = structureObjects.GetDataflows(flowRef);

            //6)	After obtaining our IDataWriterEngine, and the IDataStructureObject that we wish to create data for, we can write the dataset
            SampleDataWriter sampleDataWriter = new SampleDataWriter();
            sampleDataWriter.writeSampleData(dsd.FirstOrDefault(), dataflow.FirstOrDefault(), compactWriter);
            xmlWriter.Close();
        }
        public void TestSDMXv20(string queryFile)
        {
            IDataQuery query;
            using (IReadableDataLocation dataLocation = new FileReadableDataLocation(queryFile))
            {
                query = this._dataQueryParseManager.BuildDataQuery(dataLocation, this._retrievalManager).First();
            }
            
            var outputFileName = string.Format("{0}-out.xml", queryFile);

            using (XmlWriter writer = XmlWriter.Create(outputFileName, new XmlWriterSettings() {Indent = true}))
            {
                var compactWriter = new CompactDataWriterEngine(writer, SdmxSchema.GetFromEnum(SdmxSchemaEnumType.VersionTwo));
                this._dataRetrievalRest.GetData(query, compactWriter);
            }
            
            var selectionGroup = query.SelectionGroups.First();

            var fileInfo = new FileInfo(outputFileName);
            Assert.IsTrue(fileInfo.Exists);
            using (var fileStream = fileInfo.OpenRead())
            using (var reader = XmlReader.Create(fileStream))
            {
                TimeFormat freqValue = TimeFormat.GetFromEnum(TimeFormatEnumType.Null);
                while (reader.Read())
                {
                    switch (reader.NodeType)
                    {
                        case XmlNodeType.Element:
                            {
                                var localName = reader.LocalName;
                                if (localName.Equals("Series"))
                                {
                                    Assert.IsTrue(reader.HasAttributes);
                                    freqValue = TimeFormat.GetTimeFormatFromCodeId(reader.GetAttribute("FREQ"));
                                }
                                else if (localName.Equals("Obs"))
                                {
                                    Assert.NotNull(freqValue);
                                    Assert.IsTrue(reader.HasAttributes);
                                    var dateStr = reader.GetAttribute(DimensionObject.TimeDimensionFixedId);
                                    ISdmxDate date = new SdmxDateCore(dateStr);
                                    var dateFrom = new SdmxDateCore(new SdmxDateCore(selectionGroup.DateFrom.Date, freqValue).DateInSdmxFormat);
                                    var dateTo = new SdmxDateCore(new SdmxDateCore(selectionGroup.DateTo.Date, freqValue).DateInSdmxFormat);
                                    Assert.GreaterOrEqual(date.Date, dateFrom.Date);
                                    Assert.LessOrEqual(date.Date, dateTo.Date);
                                }
                            }

                            break;
                    }
                }
            }
        }
        public void TestGetDataDataWriterEngineEmptyAttr(string query, string name)
        {
            var connectionString = ConfigurationManager.ConnectionStrings [name];
            var dataQuery = new DataQueryImpl(new RESTDataQueryCore(query), new MappingStoreSdmxObjectRetrievalManager(connectionString));
            const string EmptyAttributeXML = "empty-attribute.xml";
            using (XmlWriter writer = XmlWriter.Create(EmptyAttributeXML, new XmlWriterSettings() {Indent = true}))
            {
                IDataWriterEngine dataWriter = new CompactDataWriterEngine(writer, SdmxSchema.GetFromEnum(SdmxSchemaEnumType.VersionTwoPointOne));

                ISdmxDataRetrievalWithWriter sdmxDataRetrievalWithWriter = new DataRetrieverCore(new HeaderImpl("TestEmptyConditionalAttribute", "ZZ9"), connectionString, SdmxSchemaEnumType.VersionTwoPointOne);
                sdmxDataRetrievalWithWriter.GetData(dataQuery, dataWriter);
                writer.Flush();
            }
            var fileInfo = new FileInfo(EmptyAttributeXML);
            Assert.IsTrue(fileInfo.Exists);
            using (var fileStream = fileInfo.OpenRead())
            using (var reader = XmlReader.Create(fileStream))
            {
                while (reader.Read())
                {
                    switch (reader.NodeType)
                    {
                        case XmlNodeType.Element:
                            {
                                var localName = reader.LocalName;
                                if (localName.Equals("Group"))
                                {
                                    Assert.IsTrue(reader.HasAttributes);
                                    var dateStr = reader.GetAttribute("NAT_TITLE");
                                    Assert.IsNull(dateStr);
                                }
                                else if (localName.Equals("Obs"))
                                {
                                    Assert.IsTrue(reader.HasAttributes);
                                    var attribute = reader.GetAttribute("OBS_COM");
                                    Assert.IsTrue(attribute == null || !string.IsNullOrWhiteSpace(attribute));
                                }
                            }

                            break;
                    }
                }
            }
        }
        public void TestNestedAndOr(string queryFile, string dataflowFile, string dsdFile, string name)
        {
            var retrievalManager = this.GetSdmxObjectRetrievalManager(dataflowFile, dsdFile);
            var connectionString = ConfigurationManager.ConnectionStrings [name];
            ISdmxDataRetrievalWithWriter dr = new DataRetrieverCore(new HeaderImpl("TestNestedAndOr", "ZZ9"), connectionString, SdmxSchemaEnumType.VersionTwo);
            IList<IDataQuery> dataQuery;
            using (var fileReadableDataLocation = new FileReadableDataLocation(queryFile))
            {
                dataQuery = this.dataQueryParseManager.BuildDataQuery(fileReadableDataLocation, retrievalManager);
                Assert.IsNotEmpty(dataQuery);
            }

            var outputFileName = string.Format("{0}-TestNestedAndOr-out.xml", queryFile);
            using (XmlWriter writer = XmlWriter.Create(outputFileName, new XmlWriterSettings { Indent = true }))
            using (IDataWriterEngine dataWriter = new CompactDataWriterEngine(writer, SdmxSchema.GetFromEnum(SdmxSchemaEnumType.VersionTwo)))
            {
                dr.GetData(dataQuery.First(), dataWriter);
            }
        }
        public void TestSDMXv20(string filePath, string name, int allowedObs)
        {
            var connectionString = ConfigurationManager.ConnectionStrings [name];
            IList<IDataQuery> dataQueries;
            var mappingStoreSdmxObjectRetrievalManager = new MappingStoreSdmxObjectRetrievalManager(connectionString);
            using (IReadableDataLocation dataLocation = new FileReadableDataLocation(filePath))
            {
                dataQueries = this._dataQueryParseManager.BuildDataQuery(dataLocation, mappingStoreSdmxObjectRetrievalManager);
            }

            Assert.IsNotEmpty(dataQueries);
            var dataQuery = dataQueries.First();

            var outputFileName = string.Format("{0}-soap-v20-max-obs-out.xml", filePath);
            using (XmlWriter writer = XmlWriter.Create(outputFileName, new XmlWriterSettings { Indent = true }))
            {
                IDataWriterEngine dataWriter = new CompactDataWriterEngine(writer, SdmxSchema.GetFromEnum(SdmxSchemaEnumType.VersionTwo));

                ISdmxDataRetrievalWithWriter advancedSdmxDataRetrievalWithWriter = new DataRetrieverCore(this._defaultHeader, connectionString, SdmxSchemaEnumType.VersionTwo, allowedObs);
                advancedSdmxDataRetrievalWithWriter.GetData(dataQuery, dataWriter);
                writer.Flush();
            }
        }
        public void TestRest(string restUrl, string connectionName, int allowedObs)
        {
            var connectionString = ConfigurationManager.ConnectionStrings [connectionName];
            var mappingStoreSdmxObjectRetrievalManager = new MappingStoreSdmxObjectRetrievalManager(connectionString);
            var dataQuery = this._dataQueryParseManager.ParseRestQuery(restUrl, mappingStoreSdmxObjectRetrievalManager);

            Assert.IsNotNull(dataQuery);

            var outputFileName = string.Format("REST-{0}-{1}-{2}-{3}--max-obsout.xml", dataQuery.Dataflow.Id, dataQuery.DimensionAtObservation, dataQuery.FirstNObservations, dataQuery.LastNObservations);
            using (XmlWriter writer = XmlWriter.Create(outputFileName, new XmlWriterSettings { Indent = true }))
            {
                IDataWriterEngine dataWriter = new CompactDataWriterEngine(writer, SdmxSchema.GetFromEnum(SdmxSchemaEnumType.VersionTwoPointOne));

                ISdmxDataRetrievalWithWriter sdmxDataRetrievalWithWriter = new DataRetrieverCore(this._defaultHeader, connectionString, SdmxSchemaEnumType.VersionTwoPointOne, allowedObs);
                sdmxDataRetrievalWithWriter.GetData(dataQuery, dataWriter);
                writer.Flush();
            }
        }
        /// <summary>
        /// The main.
        /// </summary>
        /// <param name="args">
        /// The args.
        /// </param>
        public static void Main(string[] args)
        {
            // 1. We need a IDataStructureObject. In this example we read it from a file. Alternative we could build it from a mutable object.
            IDataStructureObject dataStructure;
            using (IReadableDataLocation readable = new FileReadableDataLocation("ESTAT+STS+2.0.xml"))
            {
                IStructureWorkspace structureWorkspace = _parsingManager.ParseStructures(readable);

                ISdmxObjects structureBeans = structureWorkspace.GetStructureObjects(false);
                dataStructure = structureBeans.DataStructures.FirstOrDefault();
            }

            if (dataStructure == null)
            {
                throw new InvalidOperationException("Could not build dataStructure object");
            }

            using (XmlWriter writer = XmlWriter.Create("re-using-compact-writer.xml", new XmlWriterSettings { Indent = true }))
            {
                // initialize the data writing engine. It can be for SDMX versions 2.0 or 2.1
                IDataWriterEngine dataWriterEngine = new CompactDataWriterEngine(writer, SdmxSchema.GetFromEnum(SdmxSchemaEnumType.VersionTwo));

                // write header
                dataWriterEngine.WriteHeader(new HeaderImpl("ZZ9", "ZZ9"));

                // start dataset
                dataWriterEngine.StartDataset(null, dataStructure, null);

                // write dataset attributes
                dataWriterEngine.WriteAttributeValue("TITLE", "CompactDataWriter test");

                // write 2 group entries
                dataWriterEngine.StartGroup("SIBLING");
                dataWriterEngine.WriteGroupKeyValue("REF_AREA", "EL");
                dataWriterEngine.WriteGroupKeyValue("STS_INDICATOR", "PROD");
                dataWriterEngine.WriteGroupKeyValue("STS_ACTIVITY", "NS0030");
                dataWriterEngine.WriteGroupKeyValue("STS_INSTITUTION", "1");
                dataWriterEngine.WriteGroupKeyValue("ADJUSTMENT", "N");
                dataWriterEngine.WriteAttributeValue("COMPILATION", "test");

                dataWriterEngine.StartGroup("SIBLING");
                dataWriterEngine.WriteGroupKeyValue("REF_AREA", "EL");
                dataWriterEngine.WriteGroupKeyValue("STS_INDICATOR", "IND");
                dataWriterEngine.WriteGroupKeyValue("STS_ACTIVITY", "NS0030");
                dataWriterEngine.WriteGroupKeyValue("STS_INSTITUTION", "1");
                dataWriterEngine.WriteGroupKeyValue("ADJUSTMENT", "N");
                dataWriterEngine.WriteAttributeValue("COMPILATION", "test2");

                // write a series entry
                dataWriterEngine.StartSeries();
                dataWriterEngine.WriteSeriesKeyValue("FREQ", "A");
                dataWriterEngine.WriteSeriesKeyValue("REF_AREA", "EL");
                dataWriterEngine.WriteSeriesKeyValue("STS_INDICATOR", "PROD");
                dataWriterEngine.WriteGroupKeyValue("STS_ACTIVITY", "NS0030");
                dataWriterEngine.WriteGroupKeyValue("STS_INSTITUTION", "1");
                dataWriterEngine.WriteGroupKeyValue("ADJUSTMENT", "N");
                dataWriterEngine.WriteAttributeValue("TIME_FORMAT", "P1Y");

                // write 2 observations for the abose series
                dataWriterEngine.WriteObservation("2001", "1.23");
                dataWriterEngine.WriteAttributeValue("OBS_STATUS", "A");
                dataWriterEngine.WriteAttributeValue("OBS_CONF", "F");

                dataWriterEngine.WriteObservation("2002", "4.56");
                dataWriterEngine.WriteAttributeValue("OBS_STATUS", "A");
                dataWriterEngine.WriteAttributeValue("OBS_CONF", "F");

                // write another series entry
                dataWriterEngine.StartSeries();
                dataWriterEngine.WriteSeriesKeyValue("FREQ", "A");
                dataWriterEngine.WriteSeriesKeyValue("REF_AREA", "EL");
                dataWriterEngine.WriteSeriesKeyValue("STS_INDICATOR", "IND");
                dataWriterEngine.WriteGroupKeyValue("STS_ACTIVITY", "NS0030");
                dataWriterEngine.WriteGroupKeyValue("STS_INSTITUTION", "1");
                dataWriterEngine.WriteGroupKeyValue("ADJUSTMENT", "N");
                dataWriterEngine.WriteAttributeValue("TIME_FORMAT", "P1Y");

                // write 1 observation for the abose series
                dataWriterEngine.WriteObservation("2001", "7.89");
                dataWriterEngine.WriteAttributeValue("OBS_STATUS", "A");
                dataWriterEngine.WriteAttributeValue("OBS_CONF", "F");

                // close compact Writer
                dataWriterEngine.Close();
            }
        }