Как экспортировать все элементы checkledistbox в XML-документ в приложении windows form C#
Im not able to display all checked data in xml document only last selected Checkedlistbox item is displayed in xml i wanted to display all checked data in xml please help me in doing this..
What I have tried:
<pre> private void button2_Click(object sender, EventArgs e)
{
string str = " ";
for (int i = 0; i < checkedListBox1.Items.Count; i++)
{
if (checkedListBox1.GetItemChecked(i))
{
str = (string)checkedListBox1.Items[i];
MessageBox.Show(str);
}
}
System.IO.File.WriteAllText("GetDetails.xml", str);
XmlTextWriter xwriter = new XmlTextWriter("GetDetails.xml", Encoding.Unicode);
xwriter.WriteStartDocument();
xwriter.WriteStartElement("XMLFILE");
xwriter.WriteStartElement("file");
xwriter.WriteString(textBox1.Text);
xwriter.WriteEndElement();
// xwriter.WriteAllText(" ");
foreach (String item in checkedListBox1.SelectedItems)
{
xwriter.WriteStartElement("SelectedItems");
xwriter.WriteString(item);
xwriter.WriteEndElement();
//xwriter.WriteAllText("GetDetails.xml", str);
// File.WriteAllText("GetDetails.xml", str);
/* for (int i = 0; i < checkedListBox1.CheckedIndices.Count; i++)
{
//checkedListBox1.ClearSelected();
checkedListBox1.Items.Add(checkedListBox1.CheckedIndices[i]);
}*/
}
//File.WriteAllText("GetDetails.xml", str);
xwriter.WriteEndElement();
xwriter.WriteEndDocument();
xwriter.Close();
}