private Dictionary<string, string> MFD_Validation(GenericMetadataType Metadata, SDMXObjectModel.Message.StructureType CompleteStructure, string MFDId)
        {
            Dictionary<string, string> RetVal;
            int i;
            string MSDIdForMFD;
            RetVal = new Dictionary<string, string>();
            MSDIdForMFD = string.Empty;

            try
            {
                for (i = 0; i < CompleteStructure.Structures.Metadataflows.Count; i++)
                {
                    if (MFDId == CompleteStructure.Structures.Metadataflows[i].id)
                    {
                        MSDIdForMFD = ((MetadataStructureRefType)(CompleteStructure.Structures.Metadataflows[i].Structure.Items[0])).id;
                        break;
                    }
                }
                if (((MetadataStructureRefType)((MetadataStructureReferenceType)(Metadata.Header.Structure[0].Item)).Items[0]).id != MSDIdForMFD)
                {
                    RetVal.Add(MetadataValidationStatus.MFD_Selection_Invalid.ToString(), Constants.ValidationMessages.Invalid_MFD_Selected);
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }

            return RetVal;
        }
    private bool UpdateNonMAForUploadedDBWithHeader(string DbNId, string UserNId, string DataBaseNId)
    {
        bool RetVal;
        SDMXObjectModel.Message.RegistryInterfaceType Registrations;
        SDMXObjectModel.Message.GenericMetadataType MetadataFiles;
        //SDMXObjectModel.Message.GenericDataType DataFiles;
        SDMXObjectModel.Message.StructureSpecificTimeSeriesDataType DataFiles;

        SDMXObjectModel.Message.RegistryInterfaceType Subscriptions;
        RetVal = true;
        string UploadedHeaderFileWPath = string.Empty;
        string UploadedHeaderFolderPath = Server.MapPath("../../stock/data");
        string UploadedHeaderName = string.Empty;
        string SubscriptionsFolderPath = string.Empty;
        string MetadataFolderPath = string.Empty;
        string RegistrationsFolderPath = string.Empty;
        string SDMXMLFolderPath = string.Empty;
        string MappingFolderPath = string.Empty;
        XmlDocument UploadedHeaderXml = new XmlDocument();
        FileInfo[] Files = null;

        DirectoryInfo dirRegs = null;
        DirectoryInfo dirMetadata = null;
        DirectoryInfo dirSubscriptions = null;
        DirectoryInfo dirSDMXML = null;

        try
        {
            UploadedHeaderFileWPath = UploadedHeaderFolderPath + "/" + DataBaseNId + "/" + "sdmx" + "/" + DevInfo.Lib.DI_LibSDMX.Constants.Header.FileName;
            dirRegs = new DirectoryInfo(UploadedHeaderFolderPath + "\\" + DbNId + "\\" + "sdmx" + "\\" + "Registrations");
            dirMetadata = new DirectoryInfo(UploadedHeaderFolderPath + "\\" + DbNId + "\\" + "sdmx" + "\\" + "Metadata");
            dirSDMXML = new DirectoryInfo(UploadedHeaderFolderPath + "\\" + DbNId + "\\" + "sdmx" + "\\" + "SDMX-ML");
            dirSubscriptions = new DirectoryInfo(UploadedHeaderFolderPath + "\\" + DbNId + "\\" + "sdmx" + "\\" + "Subscriptions");
            MappingFolderPath = UploadedHeaderFolderPath + "/" + DbNId + "/" + "sdmx" + "/" + "Mappings";
            UploadedHeaderXml.Load(UploadedHeaderFileWPath);
            SDMXObjectModel.Message.StructureType UploadedDSDStructure = new SDMXObjectModel.Message.StructureType();
            SDMXObjectModel.Message.StructureHeaderType Header = new SDMXObjectModel.Message.StructureHeaderType();
            SDMXObjectModel.Message.BasicHeaderType BHeader = new SDMXObjectModel.Message.BasicHeaderType();

            UploadedDSDStructure = (SDMXObjectModel.Message.StructureType)SDMXObjectModel.Deserializer.LoadFromXmlDocument(typeof(SDMXObjectModel.Message.StructureType), UploadedHeaderXml);
            Header = UploadedDSDStructure.Header;
            //  BHeader = (SDMXObjectModel.Message.BasicHeaderType)UploadedDSDStructure.Header;
            foreach (DirectoryInfo dirReg in dirRegs.GetDirectories())
            {
                Files = dirReg.GetFiles();
                foreach (FileInfo regfile in Files)
                {
                    Registrations = new SDMXObjectModel.Message.RegistryInterfaceType();

                    Registrations = (SDMXObjectModel.Message.RegistryInterfaceType)Deserializer.LoadFromFile(typeof(SDMXObjectModel.Message.RegistryInterfaceType), UploadedHeaderFolderPath + "\\" + DbNId + "\\" + "sdmx" + "\\" + "Registrations" + "\\" + dirReg.Name + "\\" + regfile.ToString());

                    Registrations.Header.ID = Header.ID.ToString();
                    Registrations.Header.Prepared = Header.Prepared.ToString();
                    foreach (PartyType receiver in Header.Receiver)
                    {
                        Registrations.Header.Receiver.Contact = receiver.Contact;
                        Registrations.Header.Receiver.id = receiver.id;
                        Registrations.Header.Receiver.Name = receiver.Name;
                    }
                    Registrations.Header.Sender = (SDMXObjectModel.Message.SenderType)Header.Sender;
                    Registrations.Header.Test = Header.Test;

                    Registrations.Footer = null;
                    SDMXObjectModel.Serializer.SerializeToFile(typeof(SDMXObjectModel.Message.RegistryInterfaceType), Registrations, UploadedHeaderFolderPath + "\\" + DbNId + "\\" + "sdmx" + "\\" + "Registrations" + "\\" + dirReg.Name + "\\" + regfile.ToString());
                }
            }

            foreach (DirectoryInfo dirMeta in dirMetadata.GetDirectories())
            {
                Files = null;
                Files = dirMeta.GetFiles();
                foreach (FileInfo metafile in Files)
                {
                    MetadataFiles = new SDMXObjectModel.Message.GenericMetadataType();

                    MetadataFiles = (SDMXObjectModel.Message.GenericMetadataType)Deserializer.LoadFromFile(typeof(SDMXObjectModel.Message.GenericMetadataType), UploadedHeaderFolderPath + "\\" + DbNId + "\\" + "sdmx" + "\\" + "Metadata" + "\\" + dirMeta.Name + "\\" + metafile.ToString());

                    MetadataFiles.Header.ID = Header.ID.ToString();
                    MetadataFiles.Header.Prepared = Header.Prepared.ToString();
                    MetadataFiles.Header.Receiver = Header.Receiver;
                    MetadataFiles.Header.Sender = (SDMXObjectModel.Message.SenderType)Header.Sender;
                    MetadataFiles.Header.Test = Header.Test;

                    MetadataFiles.Footer = null;
                    SDMXObjectModel.Serializer.SerializeToFile(typeof(SDMXObjectModel.Message.GenericMetadataType), MetadataFiles, UploadedHeaderFolderPath + "\\" + DbNId + "\\" + "sdmx" + "\\" + "Metadata" + "\\" + dirMeta.Name + "\\" + metafile.ToString());
                }
            }

            foreach (DirectoryInfo dirSDMX in dirSDMXML.GetDirectories())
            {
                Files = null;
                Files = dirSDMX.GetFiles();
                foreach (FileInfo sdmxMlFile in Files)
                {
                    DataFiles = new SDMXObjectModel.Message.StructureSpecificTimeSeriesDataType();

                    DataFiles = (SDMXObjectModel.Message.StructureSpecificTimeSeriesDataType)Deserializer.LoadFromFile(typeof(SDMXObjectModel.Message.StructureSpecificTimeSeriesDataType), UploadedHeaderFolderPath + "\\" + DbNId + "\\" + "sdmx" + "\\" + "SDMX-ML" + "\\" + dirSDMX.Name + "\\" + sdmxMlFile.ToString());

                    DataFiles.Header.ID = Header.ID.ToString();
                    DataFiles.Header.Prepared = Header.Prepared.ToString();
                    DataFiles.Header.Receiver = Header.Receiver;
                    DataFiles.Header.Sender = (SDMXObjectModel.Message.SenderType)Header.Sender;
                    DataFiles.Header.Test = Header.Test;

                    DataFiles.Footer = null;
                    SDMXObjectModel.Serializer.SerializeToFile(typeof(SDMXObjectModel.Message.StructureSpecificTimeSeriesDataType), DataFiles, UploadedHeaderFolderPath + "\\" + DbNId + "\\" + "sdmx" + "\\" + "SDMX-ML" + "\\" + dirSDMX.Name + "\\" + sdmxMlFile.ToString());
                }
            }

            foreach (DirectoryInfo dirSubs in dirSubscriptions.GetDirectories())
            {
                Files = null;
                Files = dirSubs.GetFiles();
                foreach (FileInfo subsFile in Files)
                {
                    Subscriptions = new SDMXObjectModel.Message.RegistryInterfaceType();

                    Subscriptions = (SDMXObjectModel.Message.RegistryInterfaceType)Deserializer.LoadFromFile(typeof(SDMXObjectModel.Message.RegistryInterfaceType), UploadedHeaderFolderPath + "\\" + DbNId + "\\" + "sdmx" + "\\" + "Subscriptions" + "\\" + dirSubs.Name + "\\" + subsFile.ToString());

                    Subscriptions.Header.ID = Header.ID.ToString();
                    Subscriptions.Header.Prepared = Header.Prepared.ToString();
                    foreach (PartyType receiver in Header.Receiver)
                    {
                        Subscriptions.Header.Receiver.Contact = receiver.Contact;
                        Subscriptions.Header.Receiver.id = receiver.id;
                        Subscriptions.Header.Receiver.Name = receiver.Name;
                    }
                    Subscriptions.Header.Sender = (SDMXObjectModel.Message.SenderType)Header.Sender;
                    Subscriptions.Header.Test = Header.Test;

                    Subscriptions.Footer = null;
                    SDMXObjectModel.Serializer.SerializeToFile(typeof(SDMXObjectModel.Message.RegistryInterfaceType), Subscriptions, UploadedHeaderFolderPath + "\\" + DbNId + "\\" + "sdmx" + "\\" + "Subscriptions" + "\\" + dirSubs.Name + "\\" + subsFile.ToString());
                }
            }

        }
        catch (Exception ex)
        {
            RetVal = false;
            Global.CreateExceptionString(ex, null);
            //Global.WriteErrorsInLog("Creating CategoryScheme For Uploaded DSD From Admin");
            //Global.WriteErrorsInLog(ex.StackTrace);
            //Global.WriteErrorsInLog(ex.Message);
        }
        finally
        {

        }

        return RetVal;
    }
        private Dictionary<string, string> Referred_MSD_Validation(GenericMetadataType Metadata, SDMXObjectModel.Message.StructureType CompleteStructure)
        {
            Dictionary<string, string> RetVal;
            List<string> ListOfMSDIds;
            RetVal = new Dictionary<string, string>();
            ListOfMSDIds=new List<string>();
            try
            {
                foreach(SDMXObjectModel.Structure.MetadataStructureType MSD in  CompleteStructure.Structures.MetadataStructures)
                {
                    ListOfMSDIds.Add(MSD.id);
                }
                if (!(ListOfMSDIds.Contains(((MetadataStructureRefType)((MetadataStructureReferenceType)(Metadata.Header.Structure[0].Item)).Items[0]).id)))
                {
                    RetVal.Add(MetadataValidationStatus.Referred_MSD_Invalid.ToString(), Constants.ValidationMessages.Invalid_Referred_MSD_Id + ((MetadataStructureRefType)((MetadataStructureReferenceType)(Metadata.Header.Structure[0].Item)).Items[0]).id);
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }

            return RetVal;
        }
        private Dictionary<string, string> Validate_MSD_Compliance(GenericMetadataType Metadata, string completeFileNameWPath, string MFDId)
        {
            Dictionary<string, string> RetVal;
            SDMXObjectModel.Message.StructureType CompleteStructure;
            int i;
            RetVal = new Dictionary<string, string>();

            try
            {
                // 1. Loading the Complete file in a SDMXApi's CompleteStructure object.

                CompleteStructure = (SDMXObjectModel.Message.StructureType)(Deserializer.LoadFromFile(typeof(SDMXObjectModel.Message.StructureType), completeFileNameWPath));
                if(!(string.IsNullOrEmpty(MFDId)))
                {
                     RetVal = MFD_Validation(Metadata, CompleteStructure, MFDId);
                }
                if (RetVal.Keys.Count == 0)
                {
                    RetVal = Referred_MSD_Validation(Metadata, CompleteStructure);
                }
                if (RetVal.Keys.Count == 0)
                {

                    for (i = 0; i < CompleteStructure.Structures.MetadataStructures.Count; i++)
                    {
                        if (((MetadataStructureRefType)((MetadataStructureReferenceType)(Metadata.Header.Structure[0].Item)).Items[0]).id == CompleteStructure.Structures.MetadataStructures[i].id)
                        {
                            RetVal = Target_Validation(Metadata, CompleteStructure.Structures.MetadataStructures[i]);
                            if (RetVal.Keys.Count == 0)
                            {
                                RetVal = Report_Structure_Validation(Metadata, CompleteStructure.Structures.MetadataStructures[i]);
                            }
                            break;
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return RetVal;
        }
        private Dictionary<string, string> Validate_And_Load_Metadatafile(string MetadataFileNameWPath, out GenericMetadataType Metadata)
        {
            Dictionary<string, string> RetVal;
            XmlDocument Document;

            RetVal = new Dictionary<string, string>();
            Metadata = null;
            Document = null;

            try
            {
                // 1. Validating the Metadata file:
                RetVal = Validate_XML(MetadataFileNameWPath, out Document);

                if (RetVal.Keys.Count == 0)
                {
                    RetVal = Validate_SDMX(Document.InnerXml);
                }

                // 2. Loading the Metadata file in the GenericMetadataType object on successful validation against SDMX schemas.

                if (RetVal.Keys.Count == 0)
                {
                    Metadata = (GenericMetadataType)(Deserializer.LoadFromText(typeof(GenericMetadataType), Document.InnerXml));
                }

            }
            catch (Exception ex)
            {
                // An exception while loading the Metadata file in a GenericMetadataType object implies invalid SDMX structure.
                if (ex is System.InvalidOperationException)
                {
                    RetVal.Add(MetadataValidationStatus.SDMX_Invalid.ToString(), ex.InnerException.Message.ToString());
                }
                // All other exceptions e.g System.IO exceptions are thrown to be handled by using code.
                else
                {
                    throw ex;
                }
            }

            return RetVal;
        }
        private Dictionary<string, string> Target_Validation(GenericMetadataType Metadata, SDMXObjectModel.Structure.MetadataStructureType MSD)
        {
            Dictionary<string, string> RetVal;
            SDMXObjectModel.Structure.IdentifiableObjectTargetType IdentifiableObjectTarget;
            SDMXObjectModel.Structure.IdentifiableObjectRepresentationType LocalRepresentation;
            SDMXObjectModel.Common.ItemSchemeReferenceType Enumeration;
            SDMXObjectModel.Structure.MetadataTargetType MetadataTarget;
            SDMXObjectModel.Structure.ReportStructureType ReportStructure;
            SDMXObjectModel.Common.ObjectRefType ObjectRef;

            RetVal = new Dictionary<string, string>();
            try
            {
                MetadataTarget = new MetadataTargetType();
                MetadataTarget = (MetadataTargetType)(((SDMXObjectModel.Structure.MetadataStructureComponentsType)MSD.Item).Items[0]);
                ReportStructure = new SDMXObjectModel.Structure.ReportStructureType();
                ReportStructure = (SDMXObjectModel.Structure.ReportStructureType)(((SDMXObjectModel.Structure.MetadataStructureComponentsType)MSD.Item).Items[1]);
                IdentifiableObjectTarget = ((SDMXObjectModel.Structure.IdentifiableObjectTargetType)(MetadataTarget.Items[0]));
                LocalRepresentation = ((SDMXObjectModel.Structure.IdentifiableObjectRepresentationType)(IdentifiableObjectTarget.LocalRepresentation));
                Enumeration = ((SDMXObjectModel.Common.ItemSchemeReferenceType)(LocalRepresentation.Items[0]));

                if (Metadata.MetadataSet[0].Report[0].id == ReportStructure.id)
                {
                    if (Metadata.MetadataSet[0].Report[0].Target.id == MetadataTarget.id)
                    {
                        ObjectRef = new ObjectRefType();
                        ObjectRef=((SDMXObjectModel.Common.ObjectRefType)((SDMXObjectModel.Common.ObjectReferenceType)(Metadata.MetadataSet[0].Report[0].Target.ReferenceValue[0].Item)).Items[0]);
                        if (((ObjectRef.agencyID == ((SDMXObjectModel.Common.ItemSchemeRefType)Enumeration.Items[0]).agencyID) && (ObjectRef.maintainableParentID == ((SDMXObjectModel.Common.ItemSchemeRefType)Enumeration.Items[0]).id) && (ObjectRef.maintainableParentVersion == ((SDMXObjectModel.Common.ItemSchemeRefType)Enumeration.Items[0]).version))==false)
                        {
                            RetVal.Add(MetadataValidationStatus.Metadata_Target_Object_Reference_Invalid.ToString(), Constants.ValidationMessages.Invalid_Metadata_Target_Object_Reference);
                        }
                    }
                    else
                    {
                        RetVal.Add(MetadataValidationStatus.Metadata_Target_Invalid.ToString(), Constants.ValidationMessages.Invalid_Metadata_Target + Metadata.MetadataSet[0].Report[0].Target.id);
                    }
                }
                else
                {
                    RetVal.Add(MetadataValidationStatus.Metadata_Report_Invalid.ToString(), Constants.ValidationMessages.Invalid_Metadata_Report + Metadata.MetadataSet[0].Report[0].id);
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }

            return RetVal;
        }
        private Dictionary<string, string> Report_Structure_Validation(GenericMetadataType Metadata, SDMXObjectModel.Structure.MetadataStructureType MSD)
        {
            Dictionary<string, string> RetVal;
            List<string> ListOfReportedAttributes;
            SDMXObjectModel.Structure.ReportStructureType ReportStructure;
            SDMXObjectModel.Structure.MetadataAttributeType MetadataAttribute;
            int i;

            RetVal = new Dictionary<string, string>();
            ListOfReportedAttributes = new List<string>();
            try
            {
                ReportStructure=(SDMXObjectModel.Structure.ReportStructureType)(((SDMXObjectModel.Structure.MetadataStructureComponentsType)MSD.Item).Items[1]);
                for (i = 0; i < ReportStructure.Items.Count; i++)
                {
                    MetadataAttribute = ((SDMXObjectModel.Structure.MetadataAttributeType)(ReportStructure.Items[i]));
                    ListOfReportedAttributes.Add(MetadataAttribute.id);
                }
                for (i = 0; i < Metadata.MetadataSet[0].Report[0].AttributeSet.ReportedAttribute.Count; i++)
                {
                    if (!(ListOfReportedAttributes.Contains(Metadata.MetadataSet[0].Report[0].AttributeSet.ReportedAttribute[i].id)))
                    {
                        RetVal.Add(MetadataValidationStatus.Metadata_Report_Structure_Invalid.ToString(), Constants.ValidationMessages.Invalid_Metadata_Reported_Attribute + Metadata.MetadataSet[0].Report[0].AttributeSet.ReportedAttribute[i].id);
                        break;
                    }
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }

            return RetVal;
        }
        private GenericMetadataType Retrieve_Metadata_From_Database(MetadataTypes metadataType, string TargetObjectId,SDMXObjectModel.Message.StructureHeaderType Header)
        {
            GenericMetadataType RetVal;
            string StructureRef, ReportStructureId, MetadataTargetId, IdentifiableObjectTargetId, MaintenableParentId, MaintenableParentVersion;
            ObjectTypeCodelistType TargetObjectType;
            PackageTypeCodelistType TargetPackageType;

            RetVal = null;
            StructureRef = string.Empty;
            ReportStructureId = string.Empty;
            MetadataTargetId = string.Empty;
            IdentifiableObjectTargetId = string.Empty;
            MaintenableParentId = string.Empty;
            MaintenableParentVersion = string.Empty;
            TargetObjectType = ObjectTypeCodelistType.Code;
            TargetPackageType = PackageTypeCodelistType.codelist;

            try
            {
                switch (metadataType)
                {
                    case MetadataTypes.Area:
                        StructureRef = Constants.MSD.Area.Id;
                        ReportStructureId = Constants.MSD.Area.ReportStructureId;
                        MetadataTargetId = Constants.MSD.Area.MetadataTargetId;
                        IdentifiableObjectTargetId = Constants.MSD.Area.IdentifiableObjectTargetId;
                        MaintenableParentId = Constants.CodeList.Area.Id;
                        MaintenableParentVersion = Constants.CodeList.Area.Version;
                        TargetObjectType = ObjectTypeCodelistType.Code;
                        TargetPackageType = PackageTypeCodelistType.codelist;
                        break;
                    case MetadataTypes.Indicator:
                        StructureRef = Constants.MSD.Indicator.Id;
                        ReportStructureId = Constants.MSD.Indicator.ReportStructureId;
                        MetadataTargetId = Constants.MSD.Indicator.MetadataTargetId;
                        IdentifiableObjectTargetId = Constants.MSD.Indicator.IdentifiableObjectTargetId;
                        MaintenableParentId = Constants.CodeList.Indicator.Id;
                        MaintenableParentVersion = Constants.CodeList.Indicator.Version;
                        TargetObjectType = ObjectTypeCodelistType.Code;
                        TargetPackageType = PackageTypeCodelistType.codelist;
                        break;
                    case MetadataTypes.Source:
                        StructureRef = Constants.MSD.Source.Id;
                        ReportStructureId = Constants.MSD.Source.ReportStructureId;
                        MetadataTargetId = Constants.MSD.Source.MetadataTargetId;
                        IdentifiableObjectTargetId = Constants.MSD.Source.IdentifiableObjectTargetId;
                        MaintenableParentId = Constants.CategoryScheme.Source.Id;
                        MaintenableParentVersion = Constants.CategoryScheme.Source.Version;
                        TargetObjectType = ObjectTypeCodelistType.Category;
                        TargetPackageType = PackageTypeCodelistType.categoryscheme;
                        break;
                    case MetadataTypes.Layer:
                        StructureRef = Constants.MSD.Area.Id;
                        ReportStructureId = Constants.MSD.Area.ReportStructureId;
                        MetadataTargetId = Constants.MSD.Area.MetadataTargetId;
                        IdentifiableObjectTargetId = Constants.MSD.Area.IdentifiableObjectTargetId;
                        MaintenableParentId = Constants.CodeList.Area.Id;
                        MaintenableParentVersion = Constants.CodeList.Area.Version;
                        TargetObjectType = ObjectTypeCodelistType.Code;
                        TargetPackageType = PackageTypeCodelistType.codelist;
                        break;
                    default:
                        break;
                }

                RetVal = new GenericMetadataType();
                if (Header != null)
                {
                    RetVal.Header.ID = Header.ID;
                    RetVal.Header.Name = Header.Name;
                    RetVal.Header.Receiver = Header.Receiver;
                    RetVal.Header.Sender = Header.Sender;
                    RetVal.Header.Test = Header.Test;
                    RetVal.Header.Source = Header.Source;
                    RetVal.Header.Prepared = Header.Prepared;

                }
                else
                {
                    RetVal.Header = this.Get_Appropriate_Header(metadataType);
                }
                RetVal.Footer = null;

                RetVal.MetadataSet = new List<SDMXObjectModel.Metadata.Generic.MetadataSetType>();
                RetVal.MetadataSet.Add(new SDMXObjectModel.Metadata.Generic.MetadataSetType());
                RetVal.MetadataSet[0].structureRef = StructureRef;
                RetVal.MetadataSet[0].Annotations = null;
                RetVal.MetadataSet[0].DataProvider = null;

                RetVal.MetadataSet[0].Report = new List<SDMXObjectModel.Metadata.Generic.ReportType>();
                RetVal.MetadataSet[0].Report.Add(new SDMXObjectModel.Metadata.Generic.ReportType());
                RetVal.MetadataSet[0].Report[0].id = ReportStructureId;
                RetVal.MetadataSet[0].Report[0].Annotations = null;

                RetVal.MetadataSet[0].Report[0].Target = new SDMXObjectModel.Metadata.Generic.TargetType();
                RetVal.MetadataSet[0].Report[0].Target.id = MetadataTargetId;

                RetVal.MetadataSet[0].Report[0].Target.ReferenceValue = new List<SDMXObjectModel.Metadata.Generic.ReferenceValueType>();
                RetVal.MetadataSet[0].Report[0].Target.ReferenceValue.Add(new SDMXObjectModel.Metadata.Generic.ReferenceValueType());
                RetVal.MetadataSet[0].Report[0].Target.ReferenceValue[0].id = IdentifiableObjectTargetId;

                RetVal.MetadataSet[0].Report[0].Target.ReferenceValue[0].Item = new ObjectReferenceType();
                ((ObjectReferenceType)RetVal.MetadataSet[0].Report[0].Target.ReferenceValue[0].Item).Items = new List<object>();
                ((ObjectReferenceType)RetVal.MetadataSet[0].Report[0].Target.ReferenceValue[0].Item).Items.Add(new ObjectRefType(TargetObjectId, this._agencyId, null, MaintenableParentId, MaintenableParentVersion, TargetObjectType, true, TargetPackageType, true));

                RetVal.MetadataSet[0].Report[0].AttributeSet = this.Get_AttributeSet(metadataType);

                if (RetVal.MetadataSet[0].Report[0].AttributeSet != null && RetVal.MetadataSet[0].Report[0].AttributeSet.ReportedAttribute != null && RetVal.MetadataSet[0].Report[0].AttributeSet.ReportedAttribute.Count > 0)
                {
                    this.Fill_AttributeSet_Values(RetVal.MetadataSet[0].Report[0].AttributeSet, TargetObjectId, metadataType);
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
            }

            return RetVal;
        }