Click here to Skip to main content
15,900,907 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi..

I have CheckedListbox in Form1,i am populating it with the following code..

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


I want to bulid an XML file using these checkeditems in Form2...
Can anyone please help me ?
Posted
Updated 17-Nov-11 0:14am
v4

There are a number of ways of doing this including building the xml manually using strings, creating an xml document and parsing the checkboxlist to produce nodes, serializing the checkboxlist object etc

Have you tried something and come unstuck? If so it would be useful for you to post what you have done.
 
Share this answer
 
Comments
bhagyap 17-Nov-11 23:59pm    
Options opt=new Options();
XmlDocument document = new XmlDocument();
XmlElement rootElement = document.CreateElement("Items");
foreach (var itemObj in opt.checkedListBox1.SelectedItems)
{
XmlElement newItem = document.CreateElement("Item");
newItem.InnerText =itemObj.ToString();
rootElement.AppendChild(newItem);
}
document.AppendChild(rootElement);
document.Save("test.xml");

I hav tried this code but i am unable to fetch all the checked items..
Paul E Davies 18-Nov-11 3:35am    
I haven't been able to check your code but a couple of comments

1. I'm not sure what your object Options is, it appears to be a container for your checklist box control but by creating a new instance will your list control also be un-instantiated? If so the selecteditems property will also be empty.

2. SelectedItems will only contain the last selected item only applicable really to controls where multi select is true which cannot be with a checkedlistbox, I think you want to use the CheckedItems property which is the collection of checked items.

Not going to be able to help further until next week as I'm going to be away, but good luck, I think you're nearly there.
public delegate void Delmydata(XmlDocument xmldoc);
public Delmydata myData;

private XmlDocument dom;

private void makeDocument()
{
dom = new XmlDocument();
XmlElement rootElement = dom.CreateElement("Media");
foreach (var objElement in checkedListBox1.CheckedItems)
{
XmlElement subElement = dom.CreateElement("Folders");
subElement.InnerText = objElement.ToString();
rootElement.AppendChild(subElement);

}
//dom.Save("xml.xml");
dom.AppendChild(rootElement);
dom.Save("Xml.xml");
}

And in the next form:-

private void BuildTreeView(XmlDocument doc)
{
treeView1.Nodes.Clear();
treeView1.Nodes.Add(new TreeNode(doc.DocumentElement.Name));
TreeNode tNode = new TreeNode();
tNode = treeView1.Nodes[0];

// SECTION 3. Populate the TreeView with the DOM nodes.
AddNode(doc.DocumentElement, tNode);
treeView1.ExpandAll();

}

private void AddNode(XmlNode inXmlNode, TreeNode inTreeNode)
{
XmlNode xNode;
TreeNode tNode;
XmlNodeList nodeList;
int i;

// Loop through the XML nodes until the leaf is reached.
// Add the nodes to the TreeView during the looping process.
if (inXmlNode.HasChildNodes)
{
nodeList = inXmlNode.ChildNodes;
for (i = 0; i <= nodeList.Count - 1; i++)
{
xNode = inXmlNode.ChildNodes[i];
inTreeNode.Nodes.Add(new TreeNode(xNode.Name));
tNode = inTreeNode.Nodes[i];
AddNode(xNode, tNode);
}
}
else
{
// Here you need to pull the data from the XmlNode based on the
// type of node, whether attribute values are required, and so forth.
inTreeNode.Text = (inXmlNode.OuterXml).Trim();
}
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900