如何在Windows窗体应用程序C#中将所有checkledistbox项导出到XML文档 [英] How to export all checkledistbox items to XML document in windows form application C#

查看:165
本文介绍了如何在Windows窗体应用程序C#中将所有checkledistbox项导出到XML文档的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

            }

推荐答案

我建​​议使用 BindingSource ,参见本文中的分层数据绑定 A Detailed数据绑定教程 [ ^ ]

I can recommend using a BindingSource, see Hierarchical data binding in this article: A Detailed Data Binding Tutorial[^]
Quote:

ComboBox和ListBox通过其DataSource和DisplayMember属性绑定到List。您通常将DataSource设置为BindingSource,并将DisplayMember设置为Current对象的其中一个属性的名称。

ComboBox and ListBox bind to the List through their DataSource and DisplayMember properties. You normally set the DataSource to a BindingSource, and you set DisplayMember to the name of one of the attributes of the Current object.

使用序列化可以轻松地将Bindingsource保存为XML。

Using serialization you can easily save the Bindingsource to XML.


这篇关于如何在Windows窗体应用程序C#中将所有checkledistbox项导出到XML文档的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆