///////////////////////////////////////////////////////////////////////////////////////////////////
        ////////////BUILD FROM V2 SCHEMA                 //////////////////////////////////////////////////
        ///////////////////////////////////////////////////////////////////////////////////////////////////

        /// <summary>
        /// Initializes a new instance of the <see cref="RelatedStructuresCore"/> class.
        /// </summary>
        /// <param name="relStrucType">
        /// The rel struc type. 
        /// </param>
        /// <param name="parent">
        /// The parent. 
        /// </param>
        public RelatedStructuresCore(RelatedStructuresType relStrucType, ISdmxStructure parent)
            : base(SdmxStructureType.GetFromEnum(SdmxStructureEnumType.RelatedStructures), parent)
        {
            this._keyFamilyRef = new List<ICrossReference>();
            this._metadataStructureRef = new List<ICrossReference>();
            this._conceptSchemeRef = new List<ICrossReference>();
            this._categorySchemeRef = new List<ICrossReference>();
            this._orgSchemeRef = new List<ICrossReference>();
            this._hierCodelistRef = new List<ICrossReference>();

            // get list of key family ref
            if (relStrucType.KeyFamilyRef != null)
            {
                foreach (KeyFamilyRefType keyFam in relStrucType.KeyFamilyRef)
                {
                    if (keyFam.URN != null)
                    {
                        this._keyFamilyRef.Add(new CrossReferenceImpl(this, keyFam.URN));
                    }
                    else
                    {
                        this._keyFamilyRef.Add(
                            new CrossReferenceImpl(
                                this, 
                                keyFam.KeyFamilyAgencyID, 
                                keyFam.KeyFamilyID, 
                                keyFam.Version, 
                                SdmxStructureType.GetFromEnum(SdmxStructureEnumType.Dsd)));
                    }
                }
            }

            // get list of metadata structure ref
            if (relStrucType.MetadataStructureRef != null)
            {
                foreach (MetadataStructureRefType metStruc in relStrucType.MetadataStructureRef)
                {
                    if (metStruc.URN != null)
                    {
                        this._metadataStructureRef.Add(new CrossReferenceImpl(this, metStruc.URN));
                    }
                    else
                    {
                        this._metadataStructureRef.Add(
                            new CrossReferenceImpl(
                                this, 
                                metStruc.MetadataStructureAgencyID, 
                                metStruc.MetadataStructureID, 
                                metStruc.Version, 
                                SdmxStructureType.GetFromEnum(SdmxStructureEnumType.Msd)));
                    }
                }
            }

            // get list of concept scheme ref
            if (relStrucType.ConceptSchemeRef != null)
            {
                foreach (ConceptSchemeRefType conStruc in relStrucType.ConceptSchemeRef)
                {
                    if (conStruc.URN != null)
                    {
                        this._conceptSchemeRef.Add(new CrossReferenceImpl(this, conStruc.URN));
                    }
                    else
                    {
                        this._conceptSchemeRef.Add(
                            new CrossReferenceImpl(
                                this, 
                                conStruc.AgencyID, 
                                conStruc.ConceptSchemeID, 
                                conStruc.Version, 
                                SdmxStructureType.GetFromEnum(SdmxStructureEnumType.ConceptScheme)));
                    }
                }
            }

            // get list of category scheme ref
            if (relStrucType.CategorySchemeRef != null)
            {
                foreach (CategorySchemeRefType catStruc in relStrucType.CategorySchemeRef)
                {
                    if (catStruc.URN != null)
                    {
                        this._categorySchemeRef.Add(new CrossReferenceImpl(this, catStruc.URN));
                    }
                    else
                    {
                        this._categorySchemeRef.Add(
                            new CrossReferenceImpl(
                                this, 
                                catStruc.AgencyID, 
                                catStruc.CategorySchemeID, 
                                catStruc.Version, 
                                SdmxStructureType.GetFromEnum(SdmxStructureEnumType.CategoryScheme)));
                    }
                }
            }

            // get list of organisation scheme ref
            if (relStrucType.OrganisationSchemeRef != null)
            {
                foreach (OrganisationSchemeRefType orgScheme in relStrucType.OrganisationSchemeRef)
                {
                    if (orgScheme.URN != null)
                    {
                        this._orgSchemeRef.Add(new CrossReferenceImpl(this, orgScheme.URN));
                    }
                    else
                    {
                        this._orgSchemeRef.Add(
                            new CrossReferenceImpl(
                                this, 
                                orgScheme.AgencyID, 
                                orgScheme.OrganisationSchemeID, 
                                orgScheme.Version, 
                                SdmxStructureType.GetFromEnum(SdmxStructureEnumType.OrganisationUnitScheme)));
                    }
                }
            }

            // get list of hierarchical codelist ref
            if (relStrucType.HierarchicalCodelistRef != null)
            {
                foreach (HierarchicalCodelistRefType hierCode in relStrucType.HierarchicalCodelistRef)
                {
                    if (hierCode.URN != null)
                    {
                        this._hierCodelistRef.Add(new CrossReferenceImpl(this, hierCode.URN));
                    }
                    else
                    {
                        this._hierCodelistRef.Add(
                            new CrossReferenceImpl(
                                this, 
                                hierCode.AgencyID, 
                                hierCode.HierarchicalCodelistID, 
                                hierCode.Version, 
                                SdmxStructureType.GetFromEnum(SdmxStructureEnumType.HierarchicalCodelist)));
                    }
                }
            }
        }
        /// <summary>
        /// Process related structures.
        /// </summary>
        /// <param name="structureSetType">
        /// The structure set type.
        /// </param>
        /// <param name="buildFrom">
        /// The build from.
        /// </param>
        private static void ProcessRelatedStructures(StructureSetType structureSetType, IStructureSetObject buildFrom)
        {
            if (buildFrom.RelatedStructures != null)
            {
                var relatedStructuresType = new RelatedStructuresType();
                structureSetType.RelatedStructures = relatedStructuresType;
                IRelatedStructures relatedStructures = buildFrom.RelatedStructures;

                // KEY FAMILY REF
                foreach (ICrossReference xref in relatedStructures.DataStructureRef)
                {
                    var keyFamilyRefType = new KeyFamilyRefType();
                    relatedStructuresType.KeyFamilyRef.Add(keyFamilyRefType);
                    SetDataStructureRefAttributes(keyFamilyRefType, xref);
                }

                // METADATA STRUCTURE REF
                foreach (ICrossReference ref0 in relatedStructures.MetadataStructureRef)
                {
                    var refType = new MetadataStructureRefType();
                    relatedStructuresType.MetadataStructureRef.Add(refType);
                    SetMetadataStructureRefAttributes(refType, ref0);
                }

                // CONCEPT SCHEME REF
                foreach (ICrossReference ref1 in relatedStructures.ConceptSchemeRef)
                {
                    var refType2 = new ConceptSchemeRefType();
                    relatedStructuresType.ConceptSchemeRef.Add(refType2);
                    SetConceptSchemeRefAttributes(refType2, ref1);
                }

                // CATEGORY SCHEME REF
                foreach (ICrossReference ref3 in relatedStructures.CategorySchemeRef)
                {
                    var refType4 = new CategorySchemeRefType();
                    relatedStructuresType.CategorySchemeRef.Add(refType4);
                    SetCategorySchemeRefAttributes(refType4, ref3);
                }

                // ORGANISATION SCHEME REF
                foreach (ICrossReference ref5 in relatedStructures.OrgSchemeRef)
                {
                    var refType6 = new OrganisationSchemeRefType();
                    relatedStructuresType.OrganisationSchemeRef.Add(refType6);
                    SetOrganisationSchemeRefAttributes(refType6, ref5);
                }

                // HCL REF
                foreach (ICrossReference ref7 in relatedStructures.HierCodelistRef)
                {
                    var refType8 = new HierarchicalCodelistRefType();
                    relatedStructuresType.HierarchicalCodelistRef.Add(refType8);
                    SetHclRefAttributes(refType8, ref7);
                }
            }
        }