This is how you write textbox.Text to a file XML.
XmlTextWriter xmlFile = new XmlTextWriter("comboXml.xml", System.Text.UTF8Encoding.UTF8);
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!
source
share