Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Windows.Forms;
- using System.IO;
- using System.Xml.Serialization;
- //Install Newtonsoft with packet manager (Tools > NuGet Packet Manager > Package Manager Console)
- //PM> Install-Package Newtonsoft.Json
- using Newtonsoft.Json;
- namespace Listbox_to_XML
- {
- public partial class Form1 : Form
- {
- private string loadLocation;
- private string saveLocation;
- private List<string> tmpItems = new List<string>();
- public Form1()
- {
- InitializeComponent();
- }
- public void save_Click(object sender, EventArgs e)
- {
- /*Save dialogue formatting for selected type*/
- saveFileDialog.Title = $"Save {fileFormat.Text} File";
- saveFileDialog.Filter = $"{fileFormat.Text.ToUpper()}|*.{fileFormat.Text.ToLower()}";
- saveFileDialog.FilterIndex = 0;
- saveFileDialog.OverwritePrompt = true;
- bool save = false;
- if (saveFileDialog.ShowDialog() != DialogResult.Cancel)
- {
- saveLocation = saveFileDialog.FileName;
- save = true;
- }
- if (save)
- {
- /*Create a list with listbox items*/
- tmpItems.Clear();
- foreach (var item in listBox1.Items)
- {
- tmpItems.Add(item.ToString());
- }
- listbox lb = new listbox(); //Create an object and insert the list
- lb.items = tmpItems;
- switch (saveLocation.Split('.')[saveLocation.Split('.').Length - 1].ToUpper())
- {
- case "XML":
- StreamWriter sw = new StreamWriter(saveLocation);
- new XmlSerializer(lb.GetType()).Serialize(sw,lb); /*Write class to file as XML*/
- sw.Close();
- break;
- case "JSON":
- File.WriteAllText(saveLocation, JsonConvert.SerializeObject(lb)); /*Write class to file as JSON*/
- break;
- default:
- MessageBox.Show($"Whoops, the file format you chose .{saveLocation.Split('.')[saveLocation.Split('.').Length - 1].ToLower()} isn't supported, only XML and JSON.", "Unsupported File");
- break;
- }
- }
- }
- public void load_Click(object sender, EventArgs e)
- {
- openFileDialog.Title = "Select a File";
- openFileDialog.Filter = fileFormat.Text == "XML" ? "XML|*.xml|JSON|*.json" : "JSON|*.json|XML|*.xml";
- saveFileDialog.FilterIndex = 0;
- if (openFileDialog.ShowDialog() != DialogResult.Cancel)
- {
- loadLocation = openFileDialog.FileName;
- }
- else
- {
- loadLocation = "";
- }
- if (loadLocation != "")
- {
- listbox lb = new listbox();
- switch (loadLocation.Split('.')[loadLocation.Split('.').Length - 1].ToUpper())
- {
- case "XML":
- try
- {
- lb = (listbox)new XmlSerializer(lb.GetType()).Deserialize(new StringReader(File.ReadAllText(loadLocation)));
- populateListbox(lb.items);
- }
- catch
- {
- MessageBox.Show("File chosen is not in the correct format.", "Important");
- }
- break;
- case "JSON":
- try
- {
- lb = JsonConvert.DeserializeObject<listbox>(File.ReadAllText(loadLocation));
- populateListbox(lb.items);
- }
- catch
- {
- MessageBox.Show("File chosen is not in the correct format.", "Important");
- }
- break;
- default:
- MessageBox.Show($"Whoops, the file format you chose .{loadLocation.Split('.')[loadLocation.Split('.').Length - 1].ToLower()} isn't supported, only XML and JSON.", "Unsupported File");
- break;
- }
- }
- }
- private void populateListbox(List<string> items)
- {
- listBox1.Items.Clear();
- foreach (var item in items)
- {
- listBox1.Items.Add(item);
- }
- }
- }
- public class listbox /*Must be a parameterless constructor for XML*/
- {
- public List<string> items;
- }
- }
Add Comment
Please, Sign In to add comment