/// <summary>
        /// LinqPad calls this to display the Connection Options dialog & generate the connection
        /// properties.
        /// </summary>
        /// <param name="cxInfo">the serialized connection properties.</param>
        /// <param name="isNewConnection">True if this is a brand new connection</param>
        /// <returns>true if the connection dialog completed successfully</returns>
        public override bool ShowConnectionDialog(IConnectionInfo cxInfo, bool isNewConnection)
        {
            ConnectionProperties props;

            if (isNewConnection)
            {
                props = new ConnectionProperties();
            }
            else
            {
                props = propsSerializer.Deserialize(cxInfo.DriverData);
            }

            using (var form = new ConnectionDialog(props, isNewConnection, LoadAssemblySafely))
            {
                var result = form.ShowDialog();

                if (result == System.Windows.Forms.DialogResult.OK)
                {
                    propsSerializer.Serialize(cxInfo.DriverData, props);
                    cxInfo.DisplayName = string.Format("{0} ({1})", props.SelectedDatabase, props.ConnectionString);
                    return(true);
                }
            }

            return(false);
        }
        private void btnExport_Click(object sender, EventArgs e)
        {
            try
            {
                using (var chooser = new SaveFileDialog())
                {
                    var result = chooser.ShowDialog();

                    if (result == System.Windows.Forms.DialogResult.OK)
                    {
                        var doc = new XDocument(
                            new XDeclaration("1.0", "UTF-8", "yes")
                            );
                        var el      = new XElement("Properties");
                        var working = new ConnectionProperties();
                        this.Populate(working);

                        connectionPropertiesSerializer.Serialize(el, working);
                        doc.Add(el);

                        using (var writer = System.Xml.XmlWriter.Create(chooser.OpenFile(), new System.Xml.XmlWriterSettings
                        {
                            Indent = true,
                            CloseOutput = true
                        }))
                        {
                            doc.WriteTo(writer);
                            writer.Flush();
                        }
                    }
                }
            }catch (Exception ex)
            {
                MessageBox.Show("Export failed: " + ex);
            }
        }
        public void Serialize(XElement root, ConnectionProperties obj)
        {
            root.RemoveAll();

            root.Add(new XElement("ConnectionString", obj.ConnectionString));

            //assembly locations
            if (obj.AssemblyLocations != null)
            {
                var assemblyLocations = new XElement("AssemblyLocations");
                foreach (string s in obj.AssemblyLocations)
                {
                    assemblyLocations.Add(new XElement("Location", s));
                }
                root.Add(assemblyLocations);
            }

            //collection type mappings
            if (obj.CollectionTypeMappings != null)
            {
                var typeMappings = new XElement("CollectionTypeMappings");
                foreach (var pair in obj.CollectionTypeMappings)
                {
                    var db = new XElement("Database");
                    db.SetAttributeValue("name", pair.Key);
                    foreach (var map in pair.Value)
                    {
                        XElement mapElement = new XElement("CollectionTypeMapping");
                        ctmSerializer.Serialize(mapElement, map);
                        db.Add(mapElement);
                    }
                    typeMappings.Add(db);
                }
                root.Add(typeMappings);
            }

            //selected DB
            var selectedDb = new XElement("SelectedDb", obj.SelectedDatabase);

            root.Add(selectedDb);

            //custom serializers
            if (obj.CustomSerializers != null)
            {
                var customSerializers = new XElement("CustomSerializers");
                foreach (var pair in obj.CustomSerializers)
                {
                    var serializer = new XElement("Serializer");
                    serializer.SetAttributeValue("type", pair.Key);
                    serializer.SetAttributeValue("serializer", pair.Value);
                    customSerializers.Add(serializer);
                }
                root.Add(customSerializers);
            }

            //additional options
            if (obj.AdditionalOptions != null)
            {
                var additionalOptions = new XElement("AdditionalOptions");
                this.additionalOptionsSerializer.Serialize(additionalOptions, obj.AdditionalOptions);
                root.Add(additionalOptions);
            }

            //Initialization Query
            if (obj.InitializationQuery != null)
            {
                var query = new XElement("InitializationQuery");
                this.linqPadQuerySerializer.Serialize(query, obj.InitializationQuery);
                root.Add(query);
            }
        }