Click here to Skip to main content
15,886,518 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hello everyone

I am working on a WPF project, where i want a user to be able to insert a string into a combobox (is set to IsEditable) and save the string into an XML file. At the same time the saved string/strings has to be shown when the combobox is dropped down (also when the project is closed and opened again). I have really tried to find a solution on the internet, but without luck. Can someone please help me. So far i have tried this:

C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Xml.Serialization;
using System.IO;

namespace CIA
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        private void ButtonSaveServerName_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                XLMGetSet getset = new XLMGetSet();
                getset.serverName = ComboBoxServerName.Text;
                XMLSaveInputServer.SaveServerName(getset, "ServerNameFile.xml");
            }

            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void XML_load(object sender, EventArgs e)
        {
            if (File.Exists("ServerNameFile.xml"))
            {
                XmlSerializer xs = new XmlSerializer(typeof(XLMGetSet));
                FileStream read = new FileStream("ServerNameFile.xml", FileMode.Open, FileAccess.Read, FileShare.Read);
                XLMGetSet getset = (XLMGetSet)xs.Deserialize(read);
                ComboBoxServerName.Text = getset.serverName;
            }
        }

        private void ComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {

        }

    }
}



C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CIA
{
    public class XLMGetSet
    {
        private string ServerName;

        public string serverName
        {
            get { return ServerName; }
            set { ServerName = value; }
        }
        
    }
}


C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Serialization;
using System.IO;

namespace CIA
{
    public class XMLSaveInputServer
    {
        public static void SaveServerName(object obj, string ServerNameFile)
        {
            XmlSerializer sr = new XmlSerializer(obj.GetType());
            TextWriter write = new StreamWriter(ServerNameFile);
            sr.Serialize(write, obj);
            write.Close();
        }
    }
}
Posted

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900