c# – 在XML中为组合框添加动态值?

我创建了一个程序,它从文本框中的用户获取值,并将该值添加到ComboBox,这是正常工作,但当我关闭程序时,它丢失了所有信息因为我没有使用数组但现在我想它应该动态地向
XML添加值关闭计划后永久存储?请问我怎么能这样做

我的C#代码

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)
        {

        }
    }
}

我的XML代码

<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>

最佳答案 以下是将textbox.Text写入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;
    }

这是你如何从XML文件填充

 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");
        }

    }

现在,在MainWindow_Load事件上调用PopulateComboBox.完成!

点赞