在XML中为combobox添加动态值?

时间:2016-10-17 05:39:35

标签: c# xml combobox

我制作了一个程序,它从文本框中的用户获取价值,并将该值添加到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>
&#13;
&#13;
&#13;

1 个答案:

答案 0 :(得分:2)

以下是将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。完成!