Im無法在xml文檔中顯示所有檢查的數據只有最後 selected Checkedlistbox項目顯示在xml中我想 顯示xml中的所有檢查數據請幫助我這樣做..如何將所有checkledistbox項目導出到Windows窗體應用程序中的Xml文檔c#
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;
using System.Xml;
namespace GetDetails
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
}
private void button4_Click(object sender, EventArgs e)
{
// Create a new instance of FolderBrowserDialog.
FolderBrowserDialog folderBrowserDlg = new FolderBrowserDialog();
// A new folder button will display in FolderBrowserDialog.
folderBrowserDlg.ShowNewFolderButton = true;
//Show FolderBrowserDialog
DialogResult dlgResult = folderBrowserDlg.ShowDialog();
if (dlgResult.Equals(DialogResult.OK))
{
//Show selected folder path in textbox1.
textBox1.Text = folderBrowserDlg.SelectedPath;
//Browsing start from root folder.
Environment.SpecialFolder rootFolder =
folderBrowserDlg.RootFolder;
}
}
private void button1_Click(object sender, EventArgs e)
{
FolderBrowserDialog fbd = new FolderBrowserDialog();
if (fbd.ShowDialog() == DialogResult.OK)
{
if (!textBox1.Text.Equals(String.Empty))
{
if (System.IO.Directory.GetFiles(textBox1.Text).Length > 0)
{
foreach (string file in
System.IO.Directory.GetFiles(textBox1.Text))
{
//Add file in ListBox.
checkedListBox1.Items.Add(Path.GetFileName(file));
}
}
else
{
checkedListBox1.Items.Add(String.Format("No files Found
at location: { 0}", textBox1.Text));
}
}
/*
FolderBrowserDialog fbd = new FolderBrowserDialog();
if(fbd.ShowDialog() == DialogResult.OK)
{
checkedListBox1.Items.Clear();
string[] files = Directory.GetFiles(fbd.SelectedPath);
string[] dirs = Directory.GetDirectories(fbd.SelectedPath);
foreach(string file in files)
{
checkedListBox1.Items.Add(Path.GetFileName(file));
}
foreach (string dir in dirs)
{
checkedListBox1.Items.Add(Path.GetFileName(dir));
}
}*/
}
}
private void checkedListBox1_SelectedIndexChanged(object sender,
EventArgs e)
{
}
private void button2_Click(object sender, EventArgs e)
{
XmlTextWriter xwriter = new XmlTextWriter("GetDetails.xml",
Encoding.Unicode);
xwriter.WriteStartDocument();
xwriter.WriteStartElement("XMLFILE");
xwriter.WriteStartElement("file");
xwriter.WriteString(textBox1.Text);
xwriter.WriteEndElement();
foreach (String item in checkedListBox1.SelectedItems)
{
xwriter.WriteStartElement("SelectedItems");
xwriter.WriteString(item);
xwriter.WriteEndElement();
/* for (int i = 0; i <
checkedListBox1.CheckedIndices.Count; i++)
{
//checkedListBox1.ClearSelected();
checkedListBox1.Items.Add(checkedListBox1.CheckedIndices[i]);
}*/
}
xwriter.WriteEndElement();
xwriter.WriteEndDocument();
xwriter.Close();
}
private void button3_Click(object sender, EventArgs e)
{
this.Close();
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void button5_Click(object sender, EventArgs e)
{
this.Close();
}
}
}
only last selected Checkedlistbox item is displayed in xml i wanted to
display all checked data in xml please help me in doing this..
元素在消息框中坡平了,但是,只有最後的選擇項目導出到xml – Mounika
[Mounika](https://stackoverflow.com/users/8489669/mounika),我已經更新了我的答案。現在檢查。 –
已檢查的項目不會顯示在xml文檔中,只有最後一個選定的項目正在顯示,您是否建議我將所有選中的項目寫入xml文檔。 – Mounika