Search code examples
c#xmllistboxexport-to-xml

How to export listbox items into XML file


I'm trying to export the list box items into xml file , I'm able to export single value objects such as text box, radio button, check boxes...

<Setting Name="txt_ImageQuality">20</Setting>
<Setting Name="txt_ImageType">JPG</Setting>
<Setting Name="rdio_checkedStatus">True</Setting>

but I want to export the following listbox items into XML.

enter image description here

is there any ways around in c#, thanks for your time.


Solution

  • Could you please try the below code snippet and please vote the answer if its usefult

    XElement element = new XElement("Items");
    foreach (var item in listBox1.Items)
    {
      element.Add(new XElement("Name", item));
    }
    XDocument document = new XDocument();
    document.Add(element);
    document.Save("items.xml",SaveOptions.DisableFormatting); //create items.xml file in bin folder