oauo

Listbox to and from file (XML & JSON)

Jun 19th, 2016 (edited)
206
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.85 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Windows.Forms;
  4.  
  5. using System.IO;
  6. using System.Xml.Serialization;
  7. //Install Newtonsoft with packet manager (Tools > NuGet Packet Manager > Package Manager Console)
  8. //PM> Install-Package Newtonsoft.Json
  9. using Newtonsoft.Json;
  10.  
  11. namespace Listbox_to_XML
  12. {
  13.     public partial class Form1 : Form
  14.     {
  15.        
  16.         private string loadLocation;
  17.         private string saveLocation;
  18.         private List<string> tmpItems = new List<string>();
  19.         public Form1()
  20.         {
  21.             InitializeComponent();
  22.         }
  23.  
  24.         public void save_Click(object sender, EventArgs e)
  25.         {
  26.             /*Save dialogue formatting for selected type*/
  27.             saveFileDialog.Title = $"Save {fileFormat.Text} File";
  28.             saveFileDialog.Filter = $"{fileFormat.Text.ToUpper()}|*.{fileFormat.Text.ToLower()}";
  29.             saveFileDialog.FilterIndex = 0;
  30.             saveFileDialog.OverwritePrompt = true;
  31.             bool save = false;
  32.             if (saveFileDialog.ShowDialog() != DialogResult.Cancel)
  33.             {
  34.                 saveLocation = saveFileDialog.FileName;
  35.                 save = true;
  36.             }
  37.             if (save)
  38.             {
  39.                 /*Create a list with listbox items*/
  40.                 tmpItems.Clear();
  41.                 foreach (var item in listBox1.Items)
  42.                 {
  43.                     tmpItems.Add(item.ToString());
  44.                 }
  45.  
  46.                 listbox lb = new listbox(); //Create an object and insert the list
  47.                 lb.items = tmpItems;
  48.                 switch (saveLocation.Split('.')[saveLocation.Split('.').Length - 1].ToUpper())
  49.                 {
  50.                     case "XML":
  51.                         StreamWriter sw = new StreamWriter(saveLocation);
  52.                         new XmlSerializer(lb.GetType()).Serialize(sw,lb); /*Write class to file as XML*/
  53.                         sw.Close();
  54.                         break;
  55.                     case "JSON":
  56.                         File.WriteAllText(saveLocation, JsonConvert.SerializeObject(lb)); /*Write class to file as JSON*/
  57.                         break;
  58.                     default:
  59.                         MessageBox.Show($"Whoops, the file format you chose .{saveLocation.Split('.')[saveLocation.Split('.').Length - 1].ToLower()} isn't supported, only XML and JSON.", "Unsupported File");
  60.                         break;
  61.                 }
  62.             }
  63.         }
  64.  
  65.         public void load_Click(object sender, EventArgs e)
  66.         {
  67.             openFileDialog.Title = "Select a File";
  68.             openFileDialog.Filter = fileFormat.Text == "XML" ? "XML|*.xml|JSON|*.json" : "JSON|*.json|XML|*.xml";
  69.             saveFileDialog.FilterIndex = 0;
  70.             if (openFileDialog.ShowDialog() != DialogResult.Cancel)
  71.             {
  72.                 loadLocation = openFileDialog.FileName;
  73.             }
  74.             else
  75.             {
  76.                 loadLocation = "";
  77.             }
  78.             if (loadLocation != "")
  79.             {
  80.                 listbox lb = new  listbox();
  81.                 switch (loadLocation.Split('.')[loadLocation.Split('.').Length - 1].ToUpper())
  82.                 {
  83.                     case "XML":
  84.                         try
  85.                         {
  86.                             lb = (listbox)new XmlSerializer(lb.GetType()).Deserialize(new StringReader(File.ReadAllText(loadLocation)));
  87.                             populateListbox(lb.items);
  88.                         }
  89.                         catch
  90.                         {
  91.                             MessageBox.Show("File chosen is not in the correct format.", "Important");
  92.                         }
  93.                         break;
  94.                     case "JSON":
  95.                         try
  96.                         {
  97.                             lb = JsonConvert.DeserializeObject<listbox>(File.ReadAllText(loadLocation));
  98.                             populateListbox(lb.items);
  99.                         }
  100.                         catch
  101.                         {
  102.                             MessageBox.Show("File chosen is not in the correct format.", "Important");
  103.                         }
  104.                         break;
  105.                     default:
  106.                         MessageBox.Show($"Whoops, the file format you chose .{loadLocation.Split('.')[loadLocation.Split('.').Length - 1].ToLower()} isn't supported, only XML and JSON.", "Unsupported File");
  107.                         break;
  108.                 }
  109.             }
  110.         }
  111.         private void populateListbox(List<string> items)
  112.         {
  113.             listBox1.Items.Clear();
  114.             foreach (var item in items)
  115.             {
  116.                 listBox1.Items.Add(item);
  117.             }
  118.         }
  119.     }
  120.     public class listbox /*Must be a parameterless constructor for XML*/
  121.     {
  122.         public List<string> items;
  123.     }
  124. }
Add Comment
Please, Sign In to add comment