【问题标题】:how to export all checkledistbox items to Xml document in windows form application c#如何在windows窗体应用程序c#中将所有checkledistbox项目导出到Xml文档
【发布时间】:2017-09-06 04:19:58
【问题描述】:

我无法在 xml 文档中最后显示所有检查的数据 选中的 Checkedlistbox 项目显示在我想要的 xml 中 在 xml 中显示所有检查的数据请帮助我这样做..

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

【问题讨论】:

    标签: c# xml checkedlistbox


    【解决方案1】:

    您的问题包含正确的代码,唯一需要的更改是使用 checkedListBox1.CheckedItems 而不是 checkedListBox1.SelectedItems

    代码:

    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 (var item in checkedListBox1.CheckedItems)
        {
            xwriter.WriteStartElement("SelectedItems");
            xwriter.WriteString(item.ToString());
            xwriter.WriteEndElement();
    
        }
        xwriter.WriteEndElement();
        xwriter.WriteEndDocument();
        xwriter.Close();
    }  
    

    【讨论】:

      【解决方案2】:

      这最终变得非常简单。 checkedListBox1.Item[i] 是一个字符串值,显式转换允许将其加载到变量中。以下代码有效:

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

      现在你拿到了物品。您可以写入 XML 文件。

      用于写入 XML DOC

      System.IO.File.WriteAllText("GetDetails.xml", str);
      

      了解更多信息。转至Official Site

      【讨论】:

      • 元素在消息框中弹出,但只有最后一个选定的项目正在导出到 xml
      • Mounika ,我已经更新了我的答案。立即检查。
      • xml 文档中不显示选中的项目,仅显示最后选择的项目,请建议我将所有选中的项目写入 xml 文档。
      • 我认为你的主要问题是,你没有得到所有清单项目Mounika。在上面,str 给了你所有的checkedListBix 项目,对吗?通过 MessageBox.Show
      • 是的,我正在检查项目,想将所有检查项目导出到 xml 文档,你能在这方面帮助我吗
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-12-06
      • 2019-03-22
      • 2016-11-06
      • 1970-01-01
      相关资源
      最近更新 更多