Click here to Skip to main content
15,896,118 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

I have checkedlistbox in form1. Based on the checked items I am trying to create xml and populate same in treeview..

Following is my code:-

C#
XmlElement elmRoot = xdoc.CreateElement("Media");

foreach (FileInfo i in opt.checkedListBox1.Items)
{

    XmlElement elmfile = xdoc.CreateElement("File");

    XmlElement elmtitle = xdoc.CreateElement("Title");
    XmlText txt_title = xdoc.CreateTextNode(i.Name.ToString());

    elmRoot.AppendChild(elmfile);
    elmRoot.LastChild.AppendChild(elmtitle);
    elmRoot.LastChild.LastChild.AppendChild(txt_title);

    XmlElement elm_file_path = xdoc.CreateElement("Full-Path");
    XmlText txt_file_path = xdoc.CreateTextNode(i.FullName.ToString());

    elmRoot.LastChild.AppendChild(elm_file_path);
    elmRoot.LastChild.LastChild.AppendChild(txt_file_path);


    XmlElement elmext = xdoc.CreateElement("Extension");
    XmlText txt_ext = xdoc.CreateTextNode(i.Extension.ToString());

    elmRoot.LastChild.AppendChild(elmext);
    elmRoot.LastChild.LastChild.AppendChild(txt_ext);

}

xdoc.Save("treeviewxml.xml");



but am getting following exception:-

Invalid XML document, The document does not have a root element..


Any help is greatly appreciated.

Thanks.
Posted
Updated 9-Dec-11 0:27am
v2
Comments
Slacker007 9-Dec-11 6:27am    
Edit: Formatting
bhagyap 9-Dec-11 6:35am    
please let me know how?? i have not worked with XML before..
Slacker007 9-Dec-11 6:51am    
There are hundreds of articles and posts on using XML. You need to take the time to search and do your own research. I have to tell you that not many people here in the community are going to do the work for you.

Good luck.

1 solution

You must add the root node to the document:

xmldoc.AppendChild(elmRoot);
 
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