XML Serializing object

Published 3/29/2023 by Christian
internal void SaveSettings()
        {
            System.Windows.Forms.FolderBrowserDialog folderBrowser = new System.Windows.Forms.FolderBrowserDialog();
            if (folderBrowser.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                settingFilePath = $"{folderBrowser.SelectedPath}\\Settings.xml";
                if (File.Exists(settingFilePath))
                {
                    File.Delete(settingFilePath);
                }

                using (TextWriter writer = new StreamWriter(settingFilePath))
                {
                    System.Xml.Serialization.XmlSerializer x = new System.Xml.Serialization.XmlSerializer(SecretService.Instance.GetType());
                    x.Serialize(writer, SecretService.Instance);
                }
            }
        }
        internal void LoadSettings()
        {
            System.Windows.Forms.OpenFileDialog fileDialog = new System.Windows.Forms.OpenFileDialog();
            fileDialog.Multiselect = false;
            fileDialog.DefaultExt = "xml";
            fileDialog.Filter = "xml files (*.xml)|*.xml|All files (*.*)|*.*";

            if (fileDialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                using (StreamReader writer = new StreamReader(fileDialog.FileName))
                {
                    System.Xml.Serialization.XmlSerializer x = new System.Xml.Serialization.XmlSerializer(SecretService.Instance.GetType());
                    SecretService.Instance = x.Deserialize(writer) as SecretService;
                }
            }
        }