Add dynamic value to combobox in XML?

I made a program that takes a value from a user in a text field and adds that value to a ComboBox, which works fine, but when I closed the program, it lost all the information because it did not use an array, but now I want it to dynamically add a value XML, where is it stored permanently after closing the program? Plz help me how can i do this

MY C # Code

namespace PopupDemo
{
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        private void Show_Click(object sender, RoutedEventArgs e)
        {
            MyPopup.IsOpen = true;
        }

        private void Add_Click(object sender, RoutedEventArgs e)
        {

            comboBox.Items.Add(textbox.Text);
            MyPopup.IsOpen = false;
        }

        private void comboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {

        }
    }
}
Run codeHide result

MY XML CODE

<Window x:Class="PopupDemo.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="ToolTip Demo"
        Height="335.461"
        Width="457.092">
    <Grid Margin="10">
        <Button Click="Show_Click" Margin="82,179,68,30">Show Popup</Button>
        <Popup Name="MyPopup"
               Placement="Mouse"
               HorizontalOffset="-100"
               VerticalOffset="-100"
               AllowsTransparency="True"
               
               >
            <Grid>
                <Ellipse Width="300" Height="300" Fill="Aquamarine"/>
                <TextBox  Width="200" Height="40" Name="textbox" ></TextBox>
                <Button Click="Add_Click" Margin="77,209,63,60" Height="50" Width="150" RenderTransformOrigin="0.532,3.873">Add</Button>
            </Grid>
        </Popup>
        <ComboBox x:Name="comboBox" HorizontalAlignment="Left" Margin="82,63,0,0" VerticalAlignment="Top" Width="279" Height="83" SelectionChanged="comboBox_SelectionChanged"/>
    </Grid>
</Window>
Run codeHide result
+4
source share
1 answer

This is how you write textbox.Text to a file XML.

 //recreates file if there is already one.
    XmlTextWriter xmlFile = new XmlTextWriter("comboXml.xml", System.Text.UTF8Encoding.UTF8);


    //intened formatting
    xmlFile.Formatting = Formatting.Indented;
     private void Add_Click(object sender, RoutedEventArgs e)
            {
              comboBox.Items.Add(textbox.Text);
            try
             {
              xmlFile.WriteStartDocument(); 
              xmlFile.WriteStartElement("myData");
              xmlFile.WriteElementString("myAttribute", textbox.Text);


              xmlFile.WriteEndElement();
              xmlFile.Close();

             }
             catch (Exception ex)
              {
                 MessageBox.Show("Xml Writing Failed:" + ex.Message); 
              }


                MyPopup.IsOpen = false;
    }

and this is how you populate from the XMLfile

 private void PopulateComboBox()
    {
        DataTable dt = new DataTable();
        dt.Columns.Add("myAttribute", typeof(string));




        XmlTextReader readXml = new XmlTextReader("comboXml.xml");
        try
        {
            while (readXml.Read()) 
            {
                if (readXml.NodeType == XmlNodeType.Element)
                {
                    switch (readXml.Name)
                    {
                        case "myAttribute":
                            DataRow dr = dt.NewRow();
                            dr["myAttribute"] = readXml.ReadString() ; 
                            dt.Rows.Add(dr);
                            break;
                    }
                }
            }

            readXml.Close();
        }
        catch (Exception ex)
        {
            Console.WriteLine("Xml connection failed: " + ex.Message);
        }


        if (dt.Rows.Count > 0)
        {
            comboBox.DataSource = dt;
            comboBox.ValueMember = "myAttribute";
            comboBox.DisplayMember = "myAttribute";       
        }

        else
        {
            MessageBox.Show("No source found!", "Warning");
        }

    }

call PopulateComboBox on the MainWindow_Load event. Done!

+2
source

Source: https://habr.com/ru/post/1657957/


All Articles