public static List<string> WriteSDMXtoCSV(System.IO.Stream sdmxFile, System.IO.StreamWriter csvOutput, TranscodeTime transcode_time = null, int maxResult = 0)
        {
            //Dictionary<string, List<string>> view = new Dictionary<string, List<string>>();
            char separator = ';';
            List<string> currentRow;
            List<string> header = new List<string>();
            bool writeHeader = true;

            using (System.IO.StreamReader _stream = new System.IO.StreamReader(sdmxFile))
            {
                using (csvOutput)
                {

                    ReadableDataLocationFactory fact = new ReadableDataLocationFactory();
                    IReadableDataLocation rdl = fact.GetReadableDataLocation(_stream.BaseStream);

                    try
                    {
                        ISdmxObjectRetrievalManager retrievalManager = DataSDMX.GetManagerImmutable();

                        Org.Sdmxsource.Sdmx.DataParser.Manager.DataReaderManager dataReader = new Org.Sdmxsource.Sdmx.DataParser.Manager.DataReaderManager();
                        Org.Sdmxsource.Sdmx.Api.Engine.IDataReaderEngine dataEngine = dataReader.GetDataReaderEngine(rdl, retrievalManager);

                        if (dataEngine != null)
                        {
                            dataEngine.Reset();

                            #region Data Parsing

                            while (dataEngine.MoveNextDataset())
                            {
                                IDataStructureObject dsd = dataEngine.DataStructure;

                                List<KeyValuePair<IList<IKeyValue>, IList<IKeyValue>>> groupAttributeDef = new List<KeyValuePair<IList<IKeyValue>, IList<IKeyValue>>>();

                                Dictionary<string, int> headerIndx = new Dictionary<string, int>();
                                int i = 0;
                                foreach (var dimension in dsd.GetDimensions())
                                {
                                    header.Add(dimension.Id);
                                    headerIndx[dimension.Id] = i;
                                    i++;
                                }
                                foreach (var attribute in dsd.Attributes)
                                {
                                    header.Add(attribute.Id);
                                    headerIndx[attribute.Id] = i;
                                    i++;
                                }
                                header.Add("OBS_VALUE");
                                headerIndx["OBS_VALUE"] = i;

                                while (dataEngine.MoveNextKeyable())
                                {

                                    IKeyable currentKey = dataEngine.CurrentKey;

                                    if (currentKey.GroupName != null)
                                    {
                                        KeyValuePair<IList<IKeyValue>, IList<IKeyValue>> groupAttrAssignment =
                                            new KeyValuePair<IList<IKeyValue>, IList<IKeyValue>>(currentKey.Key, currentKey.Attributes);
                                        groupAttributeDef.Add(groupAttrAssignment);
                                    }

                                    while (dataEngine.MoveNextObservation())
                                    {
                                        currentRow = new List<string>();
                                        var currentRowArray = new string[header.Count];
                                        for (i = 0; i < currentRowArray.Length; i++) currentRowArray[i] = "";

                                        IObservation obs = dataEngine.CurrentObservation;
                                        var freq = TranscodeTime.TypePeriod.A.ToString();

                                        #region add column for key of attributes at level series
                                        foreach (IKeyValue keyValue in currentKey.Attributes)
                                        {
                                            currentRowArray[headerIndx[keyValue.Concept]] = keyValue.Code;

                                        }
                                        #endregion
                                        #region add column for key of dim
                                        foreach (IKeyValue keyValue in currentKey.Key)
                                        {
                                            currentRowArray[headerIndx[keyValue.Concept]] = keyValue.Code;

                                            if (keyValue.Concept == dsd.FrequencyDimension.Id)
                                                freq = keyValue.Code.Trim();
                                        }
                                        #endregion
                                        #region add column for key of attributes to level observation
                                        foreach (IKeyValue keyValue in obs.Attributes)
                                        {
                                            currentRowArray[headerIndx[keyValue.Concept]] = keyValue.Code;
                                        }
                                        #endregion
                                        #region add columns for key of attributes at level group
                                        foreach (var attributeDef in groupAttributeDef)
                                        {
                                            bool setAttribute = true;
                                            foreach (var condition in attributeDef.Key)
                                            {
                                                if (currentRowArray[headerIndx[condition.Concept]] != condition.Code)
                                                {
                                                    setAttribute = false;
                                                    break;
                                                }
                                            }
                                            if (setAttribute)
                                            {
                                                foreach (var attribute in attributeDef.Value)
                                                {
                                                    currentRowArray[headerIndx[attribute.Concept]] = attribute.Code;
                                                }
                                            }

                                        }

                                        #endregion
                                        #region add column Cross Sectional Value
                                        if (obs.CrossSection)
                                        {
                                            currentRowArray[headerIndx[obs.CrossSectionalValue.Concept]] = obs.CrossSectionalValue.Code.Trim();
                                        }
                                        #endregion

                                        #region Apply transcoding

                                        string obsTime = obs.ObsTime;

                                        bool useTranscode = true;
                                        if (obs.ObsTimeFormat.FrequencyCode == TranscodeTime.TypePeriod.A.ToString())
                                            useTranscode = false;

                                        if (useTranscode)
                                        {
                                            int sepIndex = obsTime.IndexOf('-');
                                            string period = string.Empty;
                                            string period_change = obsTime[sepIndex].ToString();
                                            int nextChar;

                                            if (!int.TryParse(obsTime[sepIndex + 1].ToString(), out nextChar))
                                            {
                                                period = obsTime[sepIndex + 1].ToString();
                                                period_change += period;
                                            }
                                            else period = TranscodeTime.TypePeriod.M.ToString();

                                            if (dsd.HasFrequencyDimension() && currentRowArray[headerIndx[dsd.FrequencyDimension.Id]] != null)
                                                period = currentRowArray[headerIndx[dsd.FrequencyDimension.Id]];

                                            TranscodeTime _transcode_time = new TranscodeTime();
                                            _transcode_time.stopChar = period_change;
                                            _transcode_time.periodChar = (TranscodeTime.TypePeriod)Enum.Parse(typeof(TranscodeTime.TypePeriod), period);

                                            obsTime = _transcode_time.TransCodific(obsTime);
                                        }

                                        #endregion

                                        #region add column TIME_PERIOD

                                        currentRowArray[headerIndx["TIME_PERIOD"]] = obsTime;

                                        #endregion
                                        #region add column OBS_VALUE

                                        if (obs.ObservationValue.Trim() != "NaN")
                                            currentRowArray[headerIndx["OBS_VALUE"]] = obs.ObservationValue.Trim();

                                        #endregion

                                        /*
                                        if (maxResult > 0
                                            && ((List<string>)view["OBS_VALUE"]).Count >= maxResult)
                                            return view;
                                        */

                                        if (writeHeader)
                                        {
                                            i = 0;
                                            foreach (var concept in header)
                                            {
                                                if (i > 0) csvOutput.Write(separator);
                                                csvOutput.Write(concept);
                                                i++;
                                            }
                                            csvOutput.Write(Environment.NewLine);
                                            writeHeader = false;
                                        }
                                        int j = 0;
                                        foreach (var item in currentRowArray)
                                        {
                                            if (j > 0) csvOutput.Write(separator);
                                            csvOutput.Write(item);
                                            j++;
                                        }
                                        csvOutput.Write(Environment.NewLine);
                                    }

                                }
                                break;
                            }
                            #endregion

                        }
                    }
                    catch (Exception ex)
                    {
                        //Console.Write(ex.Message);
                        throw ex;
                    }
                    finally
                    {
                        rdl.Close();
                    }
                }
            }

            //returns the list of concepts
            return header;
        }
        public static Dictionary<string, List<string>> GetView(System.IO.Stream stream, TranscodeTime transcode_time = null, int maxResult = 0)
        {
            Dictionary<string, List<string>> view = new Dictionary<string, List<string>>();

            using (System.IO.StreamReader _stream = new System.IO.StreamReader(stream))
            {
                ReadableDataLocationFactory fact = new ReadableDataLocationFactory();
                IReadableDataLocation rdl = fact.GetReadableDataLocation(_stream.BaseStream);

                try
                {
                    ISdmxObjectRetrievalManager retrievalManager = DataSDMX.GetManagerImmutable();

                    Org.Sdmxsource.Sdmx.DataParser.Manager.DataReaderManager dataReader = new Org.Sdmxsource.Sdmx.DataParser.Manager.DataReaderManager();
                    Org.Sdmxsource.Sdmx.Api.Engine.IDataReaderEngine dataEngine = dataReader.GetDataReaderEngine(rdl, retrievalManager);

                    if (dataEngine != null)
                    {
                        dataEngine.Reset();

                        #region Data Parsing

                        while (dataEngine.MoveNextDataset())
                        {
                            IDataStructureObject dsd = dataEngine.DataStructure;

                            List<KeyValuePair<IList<IKeyValue>, IList<IKeyValue>>> groupAttributeDef = new List<KeyValuePair<IList<IKeyValue>, IList<IKeyValue>>>();
                            Dictionary<string, string> groupAttrTemplate = new Dictionary<string, string>();

                            while (dataEngine.MoveNextKeyable())
                            {

                                IKeyable currentKey = dataEngine.CurrentKey;

                                if (currentKey.GroupName != null)
                                {
                                    KeyValuePair<IList<IKeyValue>, IList<IKeyValue>> groupAttrAssignment =
                                        new KeyValuePair<IList<IKeyValue>, IList<IKeyValue>>(currentKey.Key, currentKey.Attributes);
                                    groupAttributeDef.Add(groupAttrAssignment);
                                    foreach (var attr in currentKey.Attributes)
                                    {
                                        string c = attr.Concept.Trim();
                                        if (!view.Keys.Contains(c))
                                        {
                                            view.Add(c, new List<string>());
                                            groupAttrTemplate[c] = null;
                                        }
                                    }
                                }

                                while (dataEngine.MoveNextObservation())
                                {
                                    IObservation obs = dataEngine.CurrentObservation;
                                    var freq = TranscodeTime.TypePeriod.A.ToString();

                                    #region add column for key of attributes at level series
                                    foreach (IKeyValue keyValue in currentKey.Attributes)
                                    {
                                        if (view.Keys.Contains<string>(keyValue.Concept))
                                        {
                                            ((List<string>)view[keyValue.Concept]).Add(keyValue.Code.Trim());
                                        }
                                        else
                                        {
                                            view.Add(keyValue.Concept, new List<string>());
                                            ((List<string>)view[keyValue.Concept]).Add(keyValue.Code.Trim());
                                        }
                                    }
                                    #endregion
                                    #region add column for key of dim
                                    foreach (IKeyValue keyValue in currentKey.Key)
                                    {
                                        if (keyValue.Concept == dsd.FrequencyDimension.Id)
                                            freq = keyValue.Code.Trim();

                                        if (view.Keys.Contains<string>(keyValue.Concept))
                                        {
                                            ((List<string>)view[keyValue.Concept]).Add(keyValue.Code.Trim());
                                        }
                                        else
                                        {
                                            view.Add(keyValue.Concept, new List<string>());
                                            ((List<string>)view[keyValue.Concept]).Add(keyValue.Code.Trim());
                                        }
                                    }
                                    #endregion
                                    #region add column for key of attributes to level observation
                                    foreach (IKeyValue keyValue in obs.Attributes)
                                    {
                                        if (view.Keys.Contains<string>(keyValue.Concept))
                                        {
                                            ((List<string>)view[keyValue.Concept]).Add(keyValue.Code.Trim());
                                        }
                                        else
                                        {
                                            view.Add(keyValue.Concept, new List<string>());
                                            ((List<string>)view[keyValue.Concept]).Add(keyValue.Code.Trim());
                                        }
                                    }
                                    #endregion
                                    #region add columns for key of attributes at level group
                                    Dictionary<string, string> groupAttrValues = new Dictionary<string, string>(groupAttrTemplate);
                                    foreach (var attributeDef in groupAttributeDef)
                                    {
                                        bool setAttribute = true;
                                        foreach (var condition in attributeDef.Key)
                                        {
                                            var  viewItem = view[condition.Concept] as List<string>;
                                            if (viewItem == null ||
                                                viewItem.Count == 0 ||
                                                viewItem.Last() != condition.Code)
                                            {
                                                setAttribute = false;
                                                break;
                                            }
                                        }
                                        if (setAttribute)
                                        {
                                            foreach (var attribute in attributeDef.Value)
                                            {
                                                groupAttrValues[attribute.Concept] = attribute.Code;
                                            }
                                        }

                                    }
                                    foreach (var item in groupAttrValues)
                                        ((List<string>)view[item.Key]).Add(item.Value);

                                    #endregion
                                    #region add column Cross Sectional Value
                                    if (obs.CrossSection)
                                        if (view.Keys.Contains<string>(obs.CrossSectionalValue.Concept))
                                        {
                                            ((List<string>)view[obs.CrossSectionalValue.Concept]).Add(obs.CrossSectionalValue.Code.Trim());
                                        }
                                        else
                                        {
                                            view.Add(obs.CrossSectionalValue.Concept, new List<string>());
                                            ((List<string>)view[obs.CrossSectionalValue.Concept]).Add(obs.CrossSectionalValue.Code.Trim());
                                        }
                                    #endregion

                                    #region Apply transcoding

                                    string obsTime = obs.ObsTime;

                                    bool useTranscode = true;
                                    //if (view.Keys.Contains<string>("FREQ"))
                                    if (freq == TranscodeTime.TypePeriod.A.ToString()) useTranscode = false;

                                    if (useTranscode)
                                    {
                                        int sepIndex = obsTime.IndexOf('-');
                                        string period = string.Empty;
                                        string period_change = obsTime[sepIndex].ToString();
                                        int nextChar;

                                        if (!int.TryParse(obsTime[sepIndex + 1].ToString(), out nextChar))
                                        {
                                            period = obsTime[sepIndex + 1].ToString();
                                            period_change += period;
                                        }
                                        else period = TranscodeTime.TypePeriod.M.ToString();

                                        if (view.Keys.Contains<string>("FREQ")) period = freq;

                                        TranscodeTime _transcode_time = new TranscodeTime();
                                        _transcode_time.stopChar = period_change;
                                        _transcode_time.periodChar = (TranscodeTime.TypePeriod)Enum.Parse(typeof(TranscodeTime.TypePeriod), period);

                                        obsTime = _transcode_time.TransCodific(obsTime);
                                    }

                                    #endregion

                                    #region add column TIME_PERIOD

                                    if (view.Keys.Contains<string>("TIME_PERIOD"))
                                    {
                                        ((List<string>)view["TIME_PERIOD"]).Add(obsTime);
                                    }
                                    else
                                    {
                                        view.Add("TIME_PERIOD", new List<string>());
                                        ((List<string>)view["TIME_PERIOD"]).Add(obsTime);
                                    }
                                    #endregion
                                    #region add column OBS_VALUE
                                    if (view.Keys.Contains<string>("OBS_VALUE"))
                                    {
                                        ((List<string>)view["OBS_VALUE"]).Add(obs.ObservationValue.Trim());
                                    }
                                    else
                                    {
                                        view.Add("OBS_VALUE", new List<string>());
                                        ((List<string>)view["OBS_VALUE"]).Add(obs.ObservationValue.Trim());
                                    }
                                    #endregion

                                    if (maxResult > 0
                                        && ((List<string>)view["OBS_VALUE"]).Count >= maxResult)
                                        return view;

                                }

                            }
                            break;
                        }
                        #endregion

                    }
                }
                catch (Exception ex)
                {
                    //Console.Write(ex.Message);
                }

                //_stream.Close();
                rdl.Close();

            }

            //return first dataset
            return (view.Count > 0) ? view : null;
        }
        public void TestLikeNsiWcXsXsMeasure()
        {
            ICrossSectionalDataStructureObject crossSectionalDataStructureObject = BuildCrossDsdNoTimeMeasure();
            var fileInfo = new FileInfo("cross-test.TestCrossWriterXSMeasureWithOutTimeMeasure.xml");
            WriteCrossDataNoTimeMeasure(fileInfo.ToString(), crossSectionalDataStructureObject);
            IDataReaderManager manager = new DataReaderManager();
            IList<IDictionary<string, string>> dataSetStoreList = new List<IDictionary<string, string>>();
            using (var sourceData = this._factory.GetReadableDataLocation(fileInfo))
            using (var compact = manager.GetDataReaderEngine(sourceData, crossSectionalDataStructureObject, null))
            {
                while (compact.MoveNextKeyable())
                {
                    if (compact.CurrentKey.Series)
                    {
                        IList<IKeyValue> keyValues = compact.CurrentKey.Key;

                        int index = 1;
                        while (compact.MoveNextObservation())
                        {
                            var dataSetStore = new Dictionary<string, string>(StringComparer.Ordinal);
                            foreach (var key in keyValues)
                            {
                                dataSetStore.Add(key.Concept, key.Code);
                            }

                            IObservation currentObservation = compact.CurrentObservation;
                            Assert.IsNotNullOrEmpty(currentObservation.ObservationValue);
                            Assert.IsNullOrEmpty(currentObservation.ObsTime);
                            if (currentObservation.CrossSection)
                            {
                                Assert.IsNotNull(currentObservation.CrossSectionalValue);
                            }

                            dataSetStore.Add(PrimaryMeasure.FixedId, currentObservation.ObservationValue);
                            int i = int.Parse(currentObservation.ObservationValue, NumberStyles.Any, CultureInfo.InvariantCulture);
                            Assert.AreEqual(index, i);

                            index++;
                            dataSetStoreList.Add(dataSetStore);
                        }
                    }
                }
            }
        }
        public void TestDataWriterEngine(DataEnumType format)
        {
            string outfile = string.Format("{0}.xml", format);
            DataType fromEnum = DataType.GetFromEnum(format);
            var objects = GetSdmxObjects();
            IDataStructureObject dataStructureObject = objects.DataStructures.First();
            dynamic series = BuildSeries(objects);
            var dataWriterManager = new DataWriterManager();
            var startTime = new DateTime(2005, 1, 1);
            using (Stream writer = File.Create(outfile))
            {
                Stopwatch sw;
                using (IDataWriterEngine dataWriter = dataWriterManager.GetDataWriterEngine(new SdmxDataFormatCore(fromEnum), writer))
                {
                    var header = GetHeader(dataStructureObject);
                    dataWriter.WriteHeader(header);

                    dataWriter.StartDataset(null, dataStructureObject, null);
                    sw = new Stopwatch();
                    sw.Start();

                    sw.Stop();
                    Trace.WriteLine(sw.Elapsed);
                    sw.Reset();

                    sw.Start();
                    foreach (var key in series)
                    {
                        dataWriter.StartSeries();
                        dataWriter.WriteSeriesKeyValue("FREQ", key.f.Id);
                        dataWriter.WriteSeriesKeyValue("REF_AREA", "DE");
                        dataWriter.WriteSeriesKeyValue("ADJUSTMENT", key.ad.Id);
                        dataWriter.WriteSeriesKeyValue("STS_INDICATOR", "PROD");
                        dataWriter.WriteSeriesKeyValue("STS_ACTIVITY", key.ac.Id);
                        dataWriter.WriteSeriesKeyValue("STS_INSTITUTION", "1");
                        dataWriter.WriteSeriesKeyValue("STS_BASE_YEAR", "2000");
                        var getPeriod = GetPeriodFunc(key.f.Id, startTime);
                        dataWriter.WriteAttributeValue("TIME_FORMAT", GetTimeFormat(key.f.Id));

                        for (int i = 0; i < 10; i++)
                        {
                            string period = getPeriod(i);
                            dataWriter.WriteObservation(period, i.ToString(CultureInfo.InvariantCulture));
                            dataWriter.WriteAttributeValue("OBS_STATUS", "A");
                        }
                    }

                    dataWriter.Close();
                }
                sw.Stop();
                Trace.WriteLine(sw.Elapsed);
            }

            if (fromEnum.BaseDataFormat.EnumType == BaseDataFormatEnumType.Generic)
            {
                var fileReadableDataLocation = new FileReadableDataLocation(outfile);

                XMLParser.ValidateXml(fileReadableDataLocation, fromEnum.SchemaVersion);
            }

            var dataReaderManager = new DataReaderManager();
            int dataSetCount = 0;
            using (var location = new FileReadableDataLocation(outfile))
            using (var reader = dataReaderManager.GetDataReaderEngine(location, dataStructureObject, null))
            {
                while (reader.MoveNextDataset())
                {
                    Assert.IsEmpty(reader.DatasetAttributes);
                    dataSetCount++;
                    int seriesCount = 0;
                    while (reader.MoveNextKeyable())
                    {
                        var keyValues = reader.CurrentKey;
                        if (keyValues.Series)
                        {
                            dynamic expectedSeries = series[seriesCount];
                            Assert.AreEqual(keyValues.Key.First(k => k.Concept.Equals("FREQ")).Code, expectedSeries.f.Id);
                            Assert.AreEqual(keyValues.Key.First(k => k.Concept.Equals("ADJUSTMENT")).Code, expectedSeries.ad.Id);
                            Assert.AreEqual(keyValues.Key.First(k => k.Concept.Equals("STS_ACTIVITY")).Code, expectedSeries.ac.Id);
                            Assert.AreEqual(keyValues.Key.First(k => k.Concept.Equals("REF_AREA")).Code, "DE");
                            Assert.AreEqual(keyValues.Key.First(k => k.Concept.Equals("STS_INDICATOR")).Code, "PROD");
                            Assert.AreEqual(keyValues.Key.First(k => k.Concept.Equals("STS_INSTITUTION")).Code, "1");
                            Assert.AreEqual(keyValues.Key.First(k => k.Concept.Equals("STS_BASE_YEAR")).Code, "2000");
                            if (fromEnum.SchemaVersion != SdmxSchemaEnumType.Edi)
                            {
                                Assert.AreEqual(keyValues.Attributes.First(k => k.Concept.Equals("TIME_FORMAT")).Code, GetTimeFormat(expectedSeries.f.Id));
                            }

                            seriesCount++;
                            var getPeriod = GetPeriodFunc(expectedSeries.f.Id, startTime);
                            int obsCount = 0;
                            while (reader.MoveNextObservation())
                            {
                                var currentObservation = reader.CurrentObservation;
                                Assert.AreEqual(obsCount.ToString(CultureInfo.InvariantCulture), currentObservation.ObservationValue);
                                Assert.AreEqual(getPeriod(obsCount), currentObservation.ObsTime);
                                obsCount++;
                            }

                            Assert.AreEqual(10, obsCount);
                        }
                    }

                    Assert.AreEqual(series.Length, seriesCount);
                }

                Assert.AreEqual(1, dataSetCount);
            }
            
        }
        public void TestDemoFile(string dataFile, string dsdFIle)
        {
            IStructureParsingManager manager = new StructureParsingManager();
            IDataStructureObject dsd;
            using (var dsdLocation = _factory.GetReadableDataLocation(new FileInfo(dsdFIle)))
            {
                var structureWorkspace = manager.ParseStructures(dsdLocation);
                dsd = structureWorkspace.GetStructureObjects(false).DataStructures.First();

            }

            var fileInfo = new FileInfo(dataFile);
            IDataReaderManager parseManager = new DataReaderManager();
            var crossDsd = (ICrossSectionalDataStructureObject)dsd;
            var mandatoryDataSetAttributes =
                crossDsd.GetCrossSectionalAttachDataSet(true, SdmxStructureType.GetFromEnum(SdmxStructureEnumType.DataAttribute))
                    .Cast<IAttributeObject>()
                    .Where(o => o.Mandatory)
                    .ToDictionary(component => component.Id);
            using (var dataLocation = _factory.GetReadableDataLocation(fileInfo))
            {
                var dataReaderEngine = parseManager.GetDataReaderEngine(dataLocation, dsd, null);
                int datasetCount = 0;
                if (dataReaderEngine.MoveNextDataset())
                {
                    datasetCount++;
                    _log.DebugFormat("DataSet {0}", dataReaderEngine.DatasetAttributes);
                    Assert.NotNull(dataReaderEngine.DatasetAttributes);
                    
                    int keyCount = 0;
                    while (dataReaderEngine.MoveNextKeyable())
                    {
                        keyCount++;
                        var key = dataReaderEngine.CurrentKey;
                        _log.DebugFormat("Key {0}", key.Series);
                        if (key.Series)
                        {
                            int obsCount = 0;
                            while (dataReaderEngine.MoveNextObservation())
                            {
                                obsCount++;
                                
                                var obs = dataReaderEngine.CurrentObservation;
                                _log.DebugFormat("obs {0}", obs);
                                Assert.NotNull(obs);
                            }

                            Assert.AreEqual(14, obsCount);
                        }
                    }

                    Assert.AreEqual(12, keyCount);
                }

                Assert.AreEqual(1, datasetCount);
            }
        }
        public void TestLikeNsiWcAll(string file)
        {
            IDataReaderManager manager = new DataReaderManager();
            IDataStructureObject dsd = BuildDsd();
            IList<IDictionary<string, string>> dataSetStoreList = new List<IDictionary<string, string>>();
            int obscount = 0;
            using (var sourceData = this._factory.GetReadableDataLocation(new FileInfo(file)))
            using (var compact = manager.GetDataReaderEngine(sourceData, dsd, null))
            {
                int index = 0;
                while (compact.MoveNextKeyable())
                {
                    if (compact.CurrentKey.Series)
                    {
                        IList<IKeyValue> keyValues = compact.CurrentKey.Key;

                        if (index >= 100)
                        {
                            index = 0;
                        }

                        while (compact.MoveNextObservation())
                        {
                            var dataSetStore = new Dictionary<string, string>(StringComparer.Ordinal);
                            foreach (var key in keyValues)
                            {
                                dataSetStore.Add(key.Concept, key.Code);
                            }

                            IObservation currentObservation = compact.CurrentObservation;
                            Assert.IsNotNullOrEmpty(currentObservation.ObservationValue);
                            Assert.IsNotNullOrEmpty(currentObservation.ObsTime);
                            if (currentObservation.CrossSection)
                            {
                                Assert.IsNotNull(currentObservation.CrossSectionalValue);
                                dataSetStore.Add(currentObservation.CrossSectionalValue.Concept, currentObservation.CrossSectionalValue.Code);
                            }

                            dataSetStore.Add(DimensionObject.TimeDimensionFixedId, currentObservation.ObsTime);
                            ISdmxDate sdmxDate = new SdmxDateCore(currentObservation.ObsTime);
                            Assert.AreEqual(sdmxDate.TimeFormatOfDate, currentObservation.ObsTimeFormat);
                            dataSetStore.Add(PrimaryMeasure.FixedId, currentObservation.ObservationValue);
                            int i = int.Parse(currentObservation.ObservationValue, NumberStyles.Any, CultureInfo.InvariantCulture);
                            Assert.AreEqual(index, i, "Expected {0}\nBut was {1} At OBS {2}", index, i, obscount);

                            index++;
                            obscount++;
                            dataSetStoreList.Add(dataSetStore);
                        }
                    }
                }
            }
        }
 public void TestHeaderReceiver(string file, string expectedReceiver)
 {
     IDataStructureObject dsd = BuildDsd();
     var manager = new DataReaderManager();
     using (var sourceData = this._factory.GetReadableDataLocation(new FileInfo(file)))
     using (var dataReaderEngine = manager.GetDataReaderEngine(sourceData, dsd, null))
     {
         Assert.NotNull(dataReaderEngine);
         Assert.IsTrue(dataReaderEngine.MoveNextDataset());
         var header = dataReaderEngine.Header;
         Assert.NotNull(header);
         Assert.IsNotEmpty(header.Receiver);
         Assert.AreEqual(expectedReceiver, header.Receiver.First().Id);
     }
 }
        public void TestHeaderDsd(string file, string expectedDsd)
        {
            IDataStructureObject dsd = BuildDsd();
            var manager = new DataReaderManager();
            using (var sourceData = this._factory.GetReadableDataLocation(new FileInfo(file)))
            using (var dataReaderEngine = manager.GetDataReaderEngine(sourceData, dsd, null))
            {
                Assert.NotNull(dataReaderEngine);
                Assert.IsTrue(dataReaderEngine.MoveNextDataset());
                var header = dataReaderEngine.CurrentDatasetHeader;
                Assert.NotNull(header);

                Assert.AreEqual(expectedDsd, header.DataStructureReference.StructureReference.MaintainableId);
            }
        }
        public void TestLikeNSIWCVariousDataWithDsdFile(string file, string dsdFile)
        {
            IDataReaderManager manager = new DataReaderManager();
            ISdmxObjects objects;
            using (var dataLocation = this._factory.GetReadableDataLocation(new FileInfo(dsdFile)))
            {
                IStructureParsingManager parsingManager = new StructureParsingManager();
                objects = parsingManager.ParseStructures(dataLocation).GetStructureObjects(false);
            }
            var retrievalManager = new InMemoryRetrievalManager(objects); 
            IList<IDictionary<string, string>> dataSetStoreList = new List<IDictionary<string, string>>();
            using (var sourceData = this._factory.GetReadableDataLocation(new FileInfo(file)))
            using (var compact = manager.GetDataReaderEngine(sourceData, retrievalManager))
            {
                while (compact.MoveNextKeyable())
                {
                    if (compact.CurrentKey.Series)
                    {
                        IList<IKeyValue> keyValues = compact.CurrentKey.Key;

                        while (compact.MoveNextObservation())
                        {
                            var dataSetStore = new Dictionary<string, string>(StringComparer.Ordinal);
                            foreach (var key in keyValues)
                            {
                                dataSetStore.Add(key.Concept, key.Code);
                            }

                            IObservation currentObservation = compact.CurrentObservation;
                            Assert.IsNotNullOrEmpty(currentObservation.ObservationValue);
                            Assert.IsNotNullOrEmpty(currentObservation.ObsTime);
                            Assert.IsFalse(currentObservation.CrossSection);
                            dataSetStore.Add(DimensionObject.TimeDimensionFixedId, currentObservation.ObsTime);
                            ISdmxDate sdmxDate = new SdmxDateCore(currentObservation.ObsTime);
                            Assert.NotNull(sdmxDate);
                            Assert.AreEqual(sdmxDate.TimeFormatOfDate, currentObservation.ObsTimeFormat);
                            dataSetStore.Add(PrimaryMeasure.FixedId, currentObservation.ObservationValue);
                            if (!currentObservation.ObservationValue.Equals(EdiConstants.MissingVal))
                            {
                                double i;
                                Assert.IsTrue(double.TryParse(currentObservation.ObservationValue, NumberStyles.Any, CultureInfo.InvariantCulture, out i), "Cannot convert to int {0}", currentObservation.ObservationValue);
                            }

                            dataSetStoreList.Add(dataSetStore);
                        }
                    }
                }
            }
        }