public static XmlDocument FLM_Create(int DcipSiteIdentifier, X509Certificate2 x509Certificate2, string ConnectionString)
        {
            #region Set up the AMS data connection
            AMS_DataClassesDataContext amsData = null;
            try
            {
                amsData = new AMS_DataClassesDataContext(ConnectionString);
            }
            catch (Exception e)
            {
                throw new Exception("Could not connect to the database", e);
            }
            #endregion

            #region Build the core DCinemaSecurityMessage
            DCinemaSecurityMessageType extraTheatreMessage = null;
            try
            {
                extraTheatreMessage = BuildDCinemaSecurityMessage(x509Certificate2);
            }
            catch (Exception e)
            {
                throw new Exception("Error building base FLM", e);
            }
            #endregion

            #region Get AMS RequiredExtension data
            ISingleResult<flm_GetFacilityInfoResult> facilityInfo = null;
            try
            {
                facilityInfo = amsData.flm_GetFacilityInfo(DcipSiteIdentifier);
            }
            catch (Exception e)
            {
                throw new Exception("Could not get required extension data", e);
            }

            ISingleResult<flm_GetFacilitySecureEquipmentResult> facilitySecureDevices = null;
            ArrayList secureDeviceArray = null;
            try
            {
                facilitySecureDevices = amsData.flm_GetFacilitySecureEquipment(DcipSiteIdentifier);
                secureDeviceArray = new ArrayList();
                foreach (flm_GetFacilitySecureEquipmentResult secureDevice in facilitySecureDevices)
                    secureDeviceArray.Add(secureDevice);
            }
            catch (Exception e)
            {
                throw new Exception("Could not get facility secure devices", e);
            }
            #endregion

            #region Build and attach the RequiredExtension
            FLMRequiredExtensionsType flmRequiredExtention = null;
            try
            {
                flmRequiredExtention = BuildFlmRequiredExtention(x509Certificate2, facilityInfo, secureDeviceArray);
                AppendFLMRequiredExtension(extraTheatreMessage, flmRequiredExtention);
            }
            catch (Exception e)
            {
                throw new Exception("Could not build the Requred Extension", e);
            }
            #endregion

            #region Sign the DCinemaSecurityMessage
            try
            {
                SignETM(extraTheatreMessage, x509Certificate2);
                RemoveSignatureObjects(extraTheatreMessage);
            }
            catch (Exception e)
            {
                throw new Exception("Failed to sign Facility List Message", e);
            }
            #endregion

            #region Write the Extra Theatre Message to xmlDocument
            XmlSerializer xmlSerializer = new XmlSerializer(extraTheatreMessage.GetType());
            MemoryStream memStream = new MemoryStream();
            xmlSerializer.Serialize(memStream, extraTheatreMessage);
            memStream.Seek(0, System.IO.SeekOrigin.Begin);
            XmlDocument xmlDocument = new XmlDocument();
            xmlDocument.Load(memStream);
            memStream.Close();
            #endregion

            return xmlDocument;
        }
        public static XmlDocument FLMEXT_Create(int DcipSiteIdentifier, X509Certificate2 x509Certificate2, string ConnectionString)
        {
            XmlDocument xmlDocument = new XmlDocument();

            #region Set up the AMS data connection
            AMS_DataClassesDataContext amsData = null;
            try
            {
                amsData = new AMS_DataClassesDataContext(ConnectionString);
            }
            catch (Exception e)
            {
                throw new Exception("Could not connect to the database", e);
            }
            #endregion

            #region Build the core DCinemaSecurityMessage
            DCinemaSecurityMessageType extraTheatreMessage = null;
            try
            {
                extraTheatreMessage = BuildDCinemaSecurityMessage(x509Certificate2);
            }
            catch (Exception e)
            {
                throw new Exception("Error building base FLM", e);
            }
            #endregion

            #region Get AMS RequiredExtension data
            ISingleResult<flm_GetFacilityInfoResult> facilityInfo = null;
            try
            {
                facilityInfo = amsData.flm_GetFacilityInfo(DcipSiteIdentifier);
            }
            catch (Exception e)
            {
                throw new Exception("Could not get required extension data", e);
            }

            ISingleResult<flm_GetFacilitySecureEquipmentResult> facilitySecureDevices = null;
            ArrayList secureDeviceArray = null;
            try
            {
                facilitySecureDevices = amsData.flm_GetFacilitySecureEquipment(DcipSiteIdentifier);
                secureDeviceArray = new ArrayList();
                foreach (flm_GetFacilitySecureEquipmentResult secureDevice in facilitySecureDevices)
                    secureDeviceArray.Add(secureDevice);
            }
            catch (Exception e)
            {
                throw new Exception("Could not get facility secure devices", e);
            }
            #endregion

            #region Build and attach the RequiredExtension
            FLMRequiredExtensionsType flmRequiredExtention = null;
            try
            {
                flmRequiredExtention = BuildFlmRequiredExtention(x509Certificate2, facilityInfo, secureDeviceArray);
                AppendFLMRequiredExtension(extraTheatreMessage, flmRequiredExtention);
            }
            catch (Exception e)
            {
                throw new Exception("Could not build the Requred Extension", e);
            }
            #endregion

            #region Build the Non-Critical Extension with the Device Array
            ArrayList deviceArray = null;
            try
            {
                ISingleResult<flm_GetFacilityDeviceListResult> facilityDevices = amsData.flm_GetFacilityDeviceList(DcipSiteIdentifier);
                deviceArray = new ArrayList();
                foreach (flm_GetFacilityDeviceListResult device in facilityDevices)
                    deviceArray.Add(device);
            }
            catch (Exception e)
            {
                throw new Exception("Could not get facility devices", e);
            }

            FLMNonCriticalExtensionsType flmNonCriticalExtension = null;
            try
            {
                flmNonCriticalExtension = BuildFlmNonCriticalExtention(deviceArray);
            }
            catch (Exception e)
            {
                throw new Exception("Could not build Non-Critical Extension", e);
            }

            try
            {
                AppendFLMNonCriticalExtension(extraTheatreMessage, flmNonCriticalExtension);
            }
            catch (Exception e)
            {
                throw new Exception("Could not update Non-Critical Extension", e);
            }
            #endregion

            #region Build and add the NonCriticalFacilityInfo
            ISingleResult<flm_GetNonCriticalFacilityInfoResult> nonCriticalFacilityInfoData = null;
            ArrayList nonCriticalFacilityInfoList = null;
            try
            {
                nonCriticalFacilityInfoData = amsData.flm_GetNonCriticalFacilityInfo(DcipSiteIdentifier);
                nonCriticalFacilityInfoList = new ArrayList();
                foreach (flm_GetNonCriticalFacilityInfoResult NonCriticalFacilityInfoResultRow in nonCriticalFacilityInfoData)
                    nonCriticalFacilityInfoList.Add(NonCriticalFacilityInfoResultRow);
            }
            catch (Exception e)
            {
                throw new Exception("Could not get Non-Critical Facility Information", e);
            }

            flm_GetNonCriticalFacilityInfoResult nonCriticalFacilityInfoResult = (flm_GetNonCriticalFacilityInfoResult)nonCriticalFacilityInfoList[0];

            flmNonCriticalExtension.NonCriticalFacilityInfo = new FLMNonCriticalExtensionsTypeNonCriticalFacilityInfo();
            flmNonCriticalExtension.NonCriticalFacilityInfo.Circuit = nonCriticalFacilityInfoResult.Exhibitor_Name;
            flmNonCriticalExtension.NonCriticalFacilityInfo.DistributionCapabilitiyList = null;
            flmNonCriticalExtension.NonCriticalFacilityInfo.TotalScreenCount = nonCriticalFacilityInfoResult.TotalScreenCount.Value;
            flmNonCriticalExtension.NonCriticalFacilityInfo.TotalScreenCountSpecified = true;
            flmNonCriticalExtension.NonCriticalFacilityInfo.FacilityName = nonCriticalFacilityInfoResult.Site_Name;
            flmNonCriticalExtension.NonCriticalFacilityInfo.AddressList = new FLMNonCriticalExtensionsTypeNonCriticalFacilityInfoAddressList();
            flmNonCriticalExtension.NonCriticalFacilityInfo.AddressList.Address = new AddressType[1];
            flmNonCriticalExtension.NonCriticalFacilityInfo.AddressList.Address[0] = new AddressType();
            flmNonCriticalExtension.NonCriticalFacilityInfo.AddressList.Address[0].AddressClass = AddressClassType.PHYSICAL;
            flmNonCriticalExtension.NonCriticalFacilityInfo.AddressList.Address[0].StreetAddress = nonCriticalFacilityInfoResult.Street_1;
            flmNonCriticalExtension.NonCriticalFacilityInfo.AddressList.Address[0].City = nonCriticalFacilityInfoResult.City;
            flmNonCriticalExtension.NonCriticalFacilityInfo.AddressList.Address[0].State = nonCriticalFacilityInfoResult.State;
            flmNonCriticalExtension.NonCriticalFacilityInfo.AddressList.Address[0].PostalCode = nonCriticalFacilityInfoResult.Zip_Code;
            flmNonCriticalExtension.NonCriticalFacilityInfo.AddressList.Address[0].Country = nonCriticalFacilityInfoResult.Nation;

            flmNonCriticalExtension.NonCriticalFacilityInfo.FacilityUriList = new FLMNonCriticalExtensionsTypeNonCriticalFacilityInfoFacilityUriList();
            flmNonCriticalExtension.NonCriticalFacilityInfo.FacilityUriList.FacilityUri = new string[nonCriticalFacilityInfoList.Count];
            int faciltyInfoIndex = 0;
            foreach (flm_GetNonCriticalFacilityInfoResult FacilityInfo in nonCriticalFacilityInfoList)
            {
                if(FacilityInfo.Alternate_Identifier.Length > 1)
                    flmNonCriticalExtension.NonCriticalFacilityInfo.FacilityUriList.FacilityUri[faciltyInfoIndex] = "urn:x-facilityID:" + FacilityInfo.Authoritative_Source_Name.Replace(" ","") + ":" + FacilityInfo.Alternate_Identifier;
                faciltyInfoIndex++;
            }

            #endregion

            #region Get the Non-Critical Auditorium Information
            ISingleResult<flm_GetNonCriticalAuditoriumInfoResult> nonCriticalAuditoriumInfoData = null;
            ArrayList auditoriumArray = null;
            try
            {
                nonCriticalAuditoriumInfoData = amsData.flm_GetNonCriticalAuditoriumInfo(DcipSiteIdentifier);
                auditoriumArray = new ArrayList();
                foreach (flm_GetNonCriticalAuditoriumInfoResult nonCriticalAuditorimInfoRow in nonCriticalAuditoriumInfoData)
                    auditoriumArray.Add(nonCriticalAuditorimInfoRow);
            }
            catch (Exception e)
            {
                throw new Exception("Could not get Non-Critical Auditorium Information", e);
            }
            #endregion

            #region Add the Non-Critial Auditorium Information
            flmNonCriticalExtension.NonCriticalAuditoriumInfo = new FLMNonCriticalExtensionsTypeNonCriticalAuditoriumInfo();
            flmNonCriticalExtension.NonCriticalAuditoriumInfo.Auditorium = new AuditoriumType[auditoriumArray.Count];

            int auditoriumIndex = 0;
            foreach (flm_GetNonCriticalAuditoriumInfoResult auditorium in auditoriumArray)
            {
                flmNonCriticalExtension.NonCriticalAuditoriumInfo.Auditorium[auditoriumIndex] = new AuditoriumType();

                flmNonCriticalExtension.NonCriticalAuditoriumInfo.Auditorium[auditoriumIndex].AuditoriumNumber = (int)auditorium.Auditorium_Code;

                if (auditorium.Auditorium_3D.Value)
                {
                    flmNonCriticalExtension.NonCriticalAuditoriumInfo.Auditorium[auditoriumIndex].Digital3DSystem = new Digital3DSystemType();
                    //flmNonCriticalExtension.NonCriticalAuditoriumInfo.Auditorium[auditoriumIndex].Digital3DSystem.IsActive = auditorium.Acceptance_Date;
                    flmNonCriticalExtension.NonCriticalAuditoriumInfo.Auditorium[auditoriumIndex].Digital3DSystem.Digital3DConfiguration = auditorium.Type_Description;
                    if(auditorium.Date_3D_Conversion.HasValue)
                        flmNonCriticalExtension.NonCriticalAuditoriumInfo.Auditorium[auditoriumIndex].Digital3DSystem.InstallDate = auditorium.Date_3D_Conversion.Value;
                    //flmNonCriticalExtension.NonCriticalAuditoriumInfo.Auditorium[auditoriumIndex].Digital3DSystem.DecommissionDate = auditorium.
                    if(auditorium.Silver_Screen.HasValue && auditorium.Silver_Screen.Value)
                        flmNonCriticalExtension.NonCriticalAuditoriumInfo.Auditorium[auditoriumIndex].Digital3DSystem.ScreenColor = ScreenColorType.SILVER;
                    else
                        flmNonCriticalExtension.NonCriticalAuditoriumInfo.Auditorium[auditoriumIndex].Digital3DSystem.ScreenColor = ScreenColorType.WHITE;
                    //flmNonCriticalExtension.NonCriticalAuditoriumInfo.Auditorium[auditoriumIndex].Digital3DSystem.Ghostbusting
                    //flmNonCriticalExtension.NonCriticalAuditoriumInfo.Auditorium[auditoriumIndex].Digital3DSystem.GhostbustingConfiguration
                }

                if (auditorium.Acceptance_Date.HasValue)
                {
                    flmNonCriticalExtension.NonCriticalAuditoriumInfo.Auditorium[auditoriumIndex].AuditoriumInstallationDate = auditorium.Acceptance_Date.Value;
                }

                if ((auditorium.Auditorium_IMAX.HasValue) && (auditorium.Auditorium_IMAX.Value))
                {
                    flmNonCriticalExtension.NonCriticalAuditoriumInfo.Auditorium[auditoriumIndex].Integrator = "IMAX";
                }
                else if((auditorium.Conversion.HasValue) && (auditorium.Conversion.Value))
                {
                    flmNonCriticalExtension.NonCriticalAuditoriumInfo.Auditorium[auditoriumIndex].Integrator = "DCIP";
                }

                auditoriumIndex++;
            }
            #endregion

            #region Add the Non-Critical Extension to the Facility List Message
            try
            {
                AppendFLMNonCriticalExtension(extraTheatreMessage, flmNonCriticalExtension);
            }
            catch (Exception e)
            {
                throw new Exception("Failed to add the Non-Critical Extension to the Facility List Message", e);
            }
            #endregion

            #region Sign the DCinemaSecurityMessage
            try
            {
                SignETM(extraTheatreMessage, x509Certificate2);
                RemoveSignatureObjects(extraTheatreMessage);
            }
            catch (Exception e)
            {
                throw new Exception("Failed to sign Facility List Message", e);
            }
            #endregion

            #region Write the Extra Theatre Message to xmlDocument
            XmlSerializer xmlSerializer = new XmlSerializer(extraTheatreMessage.GetType());
            MemoryStream memStream = new MemoryStream();
            xmlSerializer.Serialize(memStream, extraTheatreMessage);
            memStream.Seek(0, System.IO.SeekOrigin.Begin);
            xmlDocument = new XmlDocument();
            xmlDocument.Load(memStream);
            memStream.Close();
            #endregion

            return xmlDocument;
        }