/// <summary>
        /// Loads a FDO XML configuration document
        /// </summary>
        /// <param name="xmlFile"></param>
        /// <returns></returns>
        public static FdoDataStoreConfiguration FromFile(string xmlFile)
        {
            using (var fact = new FgfGeometryFactory())
                using (var ios = new IoFileStream(xmlFile, "r"))
                {
                    using (var reader = new XmlReader(ios))
                    {
                        List <SpatialContextInfo> contexts = new List <SpatialContextInfo>();
                        using (var scReader = new XmlSpatialContextReader(reader))
                        {
                            while (scReader.ReadNext())
                            {
                                var sc = new SpatialContextInfo();
                                sc.CoordinateSystem    = scReader.GetCoordinateSystem();
                                sc.CoordinateSystemWkt = scReader.GetCoordinateSystemWkt();
                                sc.Description         = scReader.GetDescription();
                                sc.ExtentType          = scReader.GetExtentType();
                                if (sc.ExtentType == OSGeo.FDO.Commands.SpatialContext.SpatialContextExtentType.SpatialContextExtentType_Static)
                                {
                                    using (var geom = fact.CreateGeometryFromFgf(scReader.GetExtent()))
                                    {
                                        sc.ExtentGeometryText = geom.Text;
                                    }
                                }
                                sc.IsActive    = scReader.IsActive();
                                sc.Name        = scReader.GetName();
                                sc.XYTolerance = scReader.GetXYTolerance();
                                sc.ZTolerance  = scReader.GetZTolerance();

                                contexts.Add(sc);
                            }
                        }

                        ios.Reset();

                        var schemas = new FeatureSchemaCollection(null);
                        schemas.ReadXml(ios);

                        ios.Reset();

                        var mappings = new PhysicalSchemaMappingCollection();
                        mappings.ReadXml(ios);

                        ios.Close();

                        return(new FdoDataStoreConfiguration(schemas, contexts.ToArray(), mappings));
                    }
                }
        }
        private void btnSave_Click(object sender, EventArgs e)
        {
            var schema = _schema;

            //Remove elements that have been unchecked.
            foreach (TreeNode clsNode in treeSchema.Nodes)
            {
                string className = clsNode.Name;
                int    index     = schema.Classes.IndexOf(className);
                if (!clsNode.Checked)
                {
                    if (index >= 0)
                    {
                        schema.Classes.RemoveAt(index);
                    }
                }
                else
                {
                    if (index >= 0)
                    {
                        ClassDefinition clsDef = schema.Classes[index];
                        foreach (TreeNode propNode in clsNode.Nodes)
                        {
                            if (!propNode.Checked)
                            {
                                string propName = propNode.Text;
                                int    pidx     = clsDef.Properties.IndexOf(propName);
                                if (pidx >= 0)
                                {
                                    clsDef.Properties.RemoveAt(pidx);
                                    if (clsDef.IdentityProperties.Contains(propName))
                                    {
                                        int idpdx = clsDef.IdentityProperties.IndexOf(propName);
                                        clsDef.IdentityProperties.RemoveAt(idpdx);
                                    }
                                    if (clsDef.ClassType == ClassType.ClassType_FeatureClass)
                                    {
                                        FeatureClass fc = (FeatureClass)clsDef;
                                        if (fc.GeometryProperty.Name == propName)
                                        {
                                            fc.GeometryProperty = null;
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }

            if (rdXml.Checked)
            {
                using (var ios = new IoFileStream(txtXml.Text, "w"))
                {
                    using (var writer = new XmlWriter(ios, false, XmlWriter.LineFormat.LineFormat_Indent))
                    {
                        schema.WriteXml(writer);
                        writer.Close();
                    }
                    ios.Close();
                }

                MessageService.ShowMessage("Schema saved to: " + txtXml.Text);
                this.DialogResult = DialogResult.OK;
            }
            else if (rdFile.Checked)
            {
                string fileName = txtFile.Text;
                if (ExpressUtility.CreateFlatFileDataSource(fileName))
                {
                    FdoConnection conn        = ExpressUtility.CreateFlatFileConnection(fileName);
                    bool          disposeConn = true;
                    using (FdoFeatureService svc = conn.CreateFeatureService())
                    {
                        svc.ApplySchema(schema);
                        if (MessageService.AskQuestion("Schema saved to: " + txtFile.Text + " connect to it?", "Saved"))
                        {
                            FdoConnectionManager mgr = ServiceManager.Instance.GetService <FdoConnectionManager>();
                            string name = MessageService.ShowInputBox(ResourceService.GetString("TITLE_CONNECTION_NAME"), ResourceService.GetString("PROMPT_ENTER_CONNECTION"), "");
                            if (name == null)
                            {
                                return;
                            }

                            while (name == string.Empty || mgr.NameExists(name))
                            {
                                MessageService.ShowError(ResourceService.GetString("ERR_CONNECTION_NAME_EMPTY_OR_EXISTS"));
                                name = MessageService.ShowInputBox(ResourceService.GetString("TITLE_CONNECTION_NAME"), ResourceService.GetString("PROMPT_ENTER_CONNECTION"), name);

                                if (name == null)
                                {
                                    return;
                                }
                            }
                            disposeConn = false;
                            mgr.AddConnection(name, conn);
                        }
                    }
                    if (disposeConn)
                    {
                        conn.Close();
                        conn.Dispose();
                    }
                    this.DialogResult = DialogResult.OK;
                }
            }
        }
        /// <summary>
        /// Saves this out to a FDO XML configuration document
        /// </summary>
        /// <param name="xmlFile"></param>
        public void Save(string xmlFile)
        {
            using (var fact = new FgfGeometryFactory())
            using (var ws = new IoFileStream(xmlFile, "w"))
            {
                using (var writer = new XmlWriter(ws, true, XmlWriter.LineFormat.LineFormat_Indent))
                {
                    //Write spatial contexts
                    if (this.SpatialContexts != null && this.SpatialContexts.Length > 0)
                    {
                        var flags = new XmlSpatialContextFlags();
                        using (var scWriter = new XmlSpatialContextWriter(writer))
                        {
                            foreach (var sc in this.SpatialContexts)
                            {
                                //XmlSpatialContextWriter forbids writing dynamically calculated extents
                                if (sc.ExtentType == OSGeo.FDO.Commands.SpatialContext.SpatialContextExtentType.SpatialContextExtentType_Dynamic)
                                    continue;

                                scWriter.CoordinateSystem = sc.CoordinateSystem;
                                scWriter.CoordinateSystemWkt = sc.CoordinateSystemWkt;
                                scWriter.Description = sc.Description;
                                
                                scWriter.ExtentType = sc.ExtentType;

                                using (var geom = fact.CreateGeometry(sc.ExtentGeometryText))
                                {
                                    byte[] fgf = fact.GetFgf(geom);
                                    scWriter.Extent = fgf;
                                }

                                scWriter.Name = sc.Name;
                                scWriter.XYTolerance = sc.XYTolerance;
                                scWriter.ZTolerance = sc.ZTolerance;

                                scWriter.WriteSpatialContext();
                            }
                        }
                    }

                    

                    //Write logical schema
                    if (this.Schemas != null)
                    {
                        var schemas = CloneSchemas(this.Schemas);

                        schemas.WriteXml(writer);
                    }

                    //Write physical mappings
                    if (this.Mappings != null)
                    {
                        this.Mappings.WriteXml(writer);
                    }

                    writer.Close();
                }
                ws.Close();
            }
        }
        /// <summary>
        /// Loads a FDO XML configuration document
        /// </summary>
        /// <param name="xmlFile"></param>
        /// <returns></returns>
        public static FdoDataStoreConfiguration FromFile(string xmlFile)
        {
            using (var fact = new FgfGeometryFactory())
            using (var ios = new IoFileStream(xmlFile, "r"))
            {
                using (var reader = new XmlReader(ios))
                {
                    List<SpatialContextInfo> contexts = new List<SpatialContextInfo>();
                    using (var scReader = new XmlSpatialContextReader(reader))
                    {
                        while (scReader.ReadNext())
                        {
                            var sc = new SpatialContextInfo();
                            sc.CoordinateSystem = scReader.GetCoordinateSystem();
                            sc.CoordinateSystemWkt = scReader.GetCoordinateSystemWkt();
                            sc.Description = scReader.GetDescription();
                            sc.ExtentType = scReader.GetExtentType();
                            if (sc.ExtentType == OSGeo.FDO.Commands.SpatialContext.SpatialContextExtentType.SpatialContextExtentType_Static)
                            {
                                using (var geom = fact.CreateGeometryFromFgf(scReader.GetExtent()))
                                {
                                    sc.ExtentGeometryText = geom.Text;
                                }
                            }
                            sc.IsActive = scReader.IsActive();
                            sc.Name = scReader.GetName();
                            sc.XYTolerance = scReader.GetXYTolerance();
                            sc.ZTolerance = scReader.GetZTolerance();

                            contexts.Add(sc);
                        }
                    }

                    ios.Reset();

                    var schemas = new FeatureSchemaCollection(null);
                    schemas.ReadXml(ios);

                    ios.Reset();

                    var mappings = new PhysicalSchemaMappingCollection();
                    mappings.ReadXml(ios);

                    ios.Close();

                    return new FdoDataStoreConfiguration(schemas, contexts.ToArray(), mappings);
                }
            }
        }
        private void btnSave_Click(object sender, EventArgs e)
        {
            var schema = _schema;
            //Remove elements that have been unchecked. 
            foreach (TreeNode clsNode in treeSchema.Nodes)
            {
                string className = clsNode.Name;
                int index = schema.Classes.IndexOf(className);
                if (!clsNode.Checked)
                {
                    if (index >= 0)
                        schema.Classes.RemoveAt(index);
                }
                else
                {
                    if (index >= 0)
                    {
                        ClassDefinition clsDef = schema.Classes[index];
                        foreach (TreeNode propNode in clsNode.Nodes)
                        {
                            if (!propNode.Checked)
                            {
                                string propName = propNode.Text;
                                int pidx = clsDef.Properties.IndexOf(propName);
                                if (pidx >= 0)
                                {
                                    clsDef.Properties.RemoveAt(pidx);
                                    if (clsDef.IdentityProperties.Contains(propName))
                                    {
                                        int idpdx = clsDef.IdentityProperties.IndexOf(propName);
                                        clsDef.IdentityProperties.RemoveAt(idpdx);
                                    }
                                    if (clsDef.ClassType == ClassType.ClassType_FeatureClass)
                                    {
                                        FeatureClass fc = (FeatureClass)clsDef;
                                        if (fc.GeometryProperty.Name == propName)
                                        {
                                            fc.GeometryProperty = null;
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }

            if (rdXml.Checked)
            {
                using (var ios = new IoFileStream(txtXml.Text, "w"))
                {
                    using (var writer = new XmlWriter(ios, false, XmlWriter.LineFormat.LineFormat_Indent))
                    {
                        schema.WriteXml(writer);
                        writer.Close();
                    }
                    ios.Close();
                }
                
                MessageService.ShowMessage("Schema saved to: " + txtXml.Text);
                this.DialogResult = DialogResult.OK;
            }
            else if (rdFile.Checked)
            {
                string fileName = txtFile.Text;
                if (ExpressUtility.CreateFlatFileDataSource(fileName))
                {
                    FdoConnection conn = ExpressUtility.CreateFlatFileConnection(fileName);
                    bool disposeConn = true;
                    using (FdoFeatureService svc = conn.CreateFeatureService())
                    {
                        svc.ApplySchema(schema);
                        if (MessageService.AskQuestion("Schema saved to: " + txtFile.Text + " connect to it?", "Saved"))
                        {
                            FdoConnectionManager mgr = ServiceManager.Instance.GetService<FdoConnectionManager>();
                            string name = MessageService.ShowInputBox(ResourceService.GetString("TITLE_CONNECTION_NAME"), ResourceService.GetString("PROMPT_ENTER_CONNECTION"), "");
                            if (name == null)
                                return;

                            while (name == string.Empty || mgr.NameExists(name))
                            {
                                MessageService.ShowError(ResourceService.GetString("ERR_CONNECTION_NAME_EMPTY_OR_EXISTS"));
                                name = MessageService.ShowInputBox(ResourceService.GetString("TITLE_CONNECTION_NAME"), ResourceService.GetString("PROMPT_ENTER_CONNECTION"), name);

                                if (name == null)
                                    return;
                            }
                            disposeConn = false;
                            mgr.AddConnection(name, conn);
                        }
                    }
                    if (disposeConn)
                    {
                        conn.Close();
                        conn.Dispose();
                    }
                    this.DialogResult = DialogResult.OK;
                }
            }
        }
        /// <summary>
        /// Saves this out to a FDO XML configuration document
        /// </summary>
        /// <param name="xmlFile"></param>
        public void Save(string xmlFile)
        {
            using (var fact = new FgfGeometryFactory())
                using (var ws = new IoFileStream(xmlFile, "w"))
                {
                    using (var writer = new XmlWriter(ws, true, XmlWriter.LineFormat.LineFormat_Indent))
                    {
                        //Write spatial contexts
                        if (this.SpatialContexts != null && this.SpatialContexts.Length > 0)
                        {
                            var flags = new XmlSpatialContextFlags();
                            using (var scWriter = new XmlSpatialContextWriter(writer))
                            {
                                foreach (var sc in this.SpatialContexts)
                                {
                                    //XmlSpatialContextWriter forbids writing dynamically calculated extents
                                    if (sc.ExtentType == OSGeo.FDO.Commands.SpatialContext.SpatialContextExtentType.SpatialContextExtentType_Dynamic)
                                    {
                                        continue;
                                    }

                                    scWriter.CoordinateSystem    = sc.CoordinateSystem;
                                    scWriter.CoordinateSystemWkt = sc.CoordinateSystemWkt;
                                    scWriter.Description         = sc.Description;

                                    scWriter.ExtentType = sc.ExtentType;

                                    using (var geom = fact.CreateGeometry(sc.ExtentGeometryText))
                                    {
                                        byte[] fgf = fact.GetFgf(geom);
                                        scWriter.Extent = fgf;
                                    }

                                    scWriter.Name        = sc.Name;
                                    scWriter.XYTolerance = sc.XYTolerance;
                                    scWriter.ZTolerance  = sc.ZTolerance;

                                    scWriter.WriteSpatialContext();
                                }
                            }
                        }



                        //Write logical schema
                        if (this.Schemas != null)
                        {
                            var schemas = CloneSchemas(this.Schemas);

                            schemas.WriteXml(writer);
                        }

                        //Write physical mappings
                        if (this.Mappings != null)
                        {
                            this.Mappings.WriteXml(writer);
                        }

                        writer.Close();
                    }
                    ws.Close();
                }
        }