Search code examples
c#asp.netxmlexport-to-xml

C# - Exporting listbox contents to XML


I have a list box control that contains key value pairs delimited by an "=" sign.

Example:

hot=cold

fast=slow

high=low

blue=red

I also have a button that will allow user to export this list in XML. How could I easily do that?

How do I crat the XML file what format should it be in?


Solution

  • You can use LINQ:

    var xml = new XElement("Items",
        from s in strings 
        let parts = s.Split('=')
        select new XElement("Item", 
            new XAttribute("Key", parts[0]), 
            parts[1]
        )
    );