Hi..

I have checkedlistbox in Form1,i am populating it using the following code

System.IO.DirectoryInfo di = new System.IO.DirectoryInfo("E:\\Testing");
System.IO.FileSystemInfo[] files = di.GetDirectories();
checkedListBox1.Items.AddRange(files);

now i want to bulid an xml file in Form2 using the checkeditems in Form1 for which i have tried with the following code

Options opt = new Options();
            XmlDocument document = new XmlDocument();
            XmlElement rootElement = document.CreateElement("Items");
            foreach (char itemObj in opt.checkedListBox1.CheckedItems)
            {
                XmlElement newItem = document.CreateElement("Item");
                newItem.InnerText = itemObj.ToString();
                rootElement.AppendChild(newItem);
            }
            document.AppendChild(rootElement);
            document.Save("test.xml");

Can anyone please help me with this???

The foreach structure will analyze the checked items that will return objects, not char. I will recommend to use var to let the C# define the type of.

foreach (var itemObj in opt.checkedListBox1.CheckedItems

Then when accessing to the object, mostly you'll need to cast it instead of using the ToString() that, in this case, will return the class name of the object.

My be you can try some thing like

newItem.InnerText = (string) itemObj;

Hope this helps

Hi.. I tried with your code but it hardly helped me;Values are not getting passed into foreach....

What is doing

Options opt = new Options();

If this does not fills the opt.checkedListBox1 with some checked items, then the for-each will do nothing.

Can you be so kind to post your full code here?

System.IO.DirectoryInfo di = new System.IO.DirectoryInfo("E:\\Testing");
 System.IO.FileSystemInfo[] files = di.GetDirectories();
 checkedListBox1.Items.AddRange(files);
 

Options opt = new Options();
 XmlDocument document = new XmlDocument();
 XmlElement rootElement = document.CreateElement("Items");
 foreach (var itemObj in opt.checkedListBox1.CheckedItems)
 {
 XmlElement newItem = document.CreateElement("Item");
 newItem.InnerText =itemObj.ToString();
 rootElement.AppendChild(newItem);
 }
 document.AppendChild(rootElement);
 document.Save("test.xml");

This is what i am trying with..

Thank You..

Sorry if I do not understand what is doing the line of code

Options opt = new Options();

Can you be so kind to put the full code for class Options here?
Specially I Am interested on the void New().

Thanks in advance.

Sorry if I do not understand what is doing the line of code

Options opt = new Options();

Can you be so kind to put the full code for class Options here?
Specially I Am interested on the void New().

Thanks in advance.

My CheckedListBox is in the different form by name "Options" so i have changed the modifiers to public and using it in another form..

Ok. According to the code shown on this thread, you instantiate a new form Options, but is not shown yet.

In this code, the Options.ShowDialog does not appear, so we can infere that the CheckedListBox, even in the case that you fill it in the New sub of the form Options, has no checked item yet, unless you set some of them explicitely.

Then you try to find the CheckedListBox.CheckedItems. Obviously there are none.

Maybe I am wrong, but try to show the form (dialog mode) first.

Hope this helps.

Ok. According to the code shown on this thread, you instantiate a new form Options, but is not shown yet.

In this code, the Options.ShowDialog does not appear, so we can infere that the CheckedListBox, even in the case that you fill it in the New sub of the form Options, has no checked item yet, unless you set some of them explicitely.

Then you try to find the CheckedListBox.CheckedItems. Obviously there are none.

Maybe I am wrong, but try to show the form (dialog mode) first.

Hope this helps.

Actually the Options form opens when the Form1 loads and where i check some items and close it then using those checked items i am trying to create XML and populate the treeview..

System.IO.DirectoryInfo di = new System.IO.DirectoryInfo("E:\\Testing");
System.IO.FileSystemInfo[] files = di.GetDirectories();
checkedListBox1.Items.AddRange(files);

Options opt = new Options();
XmlDocument document = new XmlDocument();
XmlElement rootElement = document.CreateElement("Items");
foreach (var itemObj in opt.checkedListBox1.CheckedItems)
{
XmlElement newItem = document.CreateElement("Item");
newItem.InnerText =itemObj.ToString();
rootElement.AppendChild(newItem);
}
document.AppendChild(rootElement);
document.Save("test.xml");

hi, error at code?! " Options opt = new Options();"

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.