public void SerializeXML(Potato potato) { XmlSerializer serializer = new XmlSerializer(typeof(Potato)); TextWriter writer = new StreamWriter(@"C:\temp\XMLPotato.xml"); serializer.Serialize(writer, potato); writer.Close(); }
public void SerializeJSON(Potato potato) { JsonSerializer serializer = new JsonSerializer(); serializer.Converters.Add(new JavaScriptDateTimeConverter()); serializer.NullValueHandling = NullValueHandling.Ignore; using (StreamWriter sw = new StreamWriter(@"C:\temp\jsonPotato.json")) using (JsonWriter writer = new JsonTextWriter(sw)) { serializer.Serialize(writer, potato); } }
public void SerializeBinary(Potato potato) { FileStream fs = new FileStream(@"C:\temp\BinaryPotato.dat", FileMode.Create); // Construct a BinaryFormatter and use it to serialize the data to the stream. BinaryFormatter formatter = new BinaryFormatter(); try { formatter.Serialize(fs, potato); } catch (SerializationException e) { Console.WriteLine("Failed to serialize. Reason: " + e.Message); throw; } finally { fs.Close(); } }
public Potato DeserializeBinary() { FileStream fs = new FileStream(@"C:\temp\BinaryPotato.dat", FileMode.Open); // Construct a BinaryFormatter and use it to serialize the data to the stream. BinaryFormatter formatter = new BinaryFormatter(); try { Potato result = (Potato)formatter.Deserialize(fs); return(result); } catch (SerializationException e) { Console.WriteLine("Failed to serialize. Reason: " + e.Message); throw; } finally { fs.Close(); } }
private void DoSomething(Potato p) { Console.WriteLine(p); }
public void ListExample() { List <Potato> potatoes = new List <Potato>() { new Potato() { Color = "Blue", Shape = "Square", Size = 200, SnelKokend = false }, new Potato() { Color = "Blue", Shape = "Square", Size = 30, SnelKokend = false }, new Potato() { Color = "Red", Shape = "Square", Size = 100, SnelKokend = false }, new Potato() { Color = "Yellow", Shape = "Square", Size = 10, SnelKokend = false } }; potatoes.Add(new Potato() { Color = "Orange", Shape = "Square", Size = 50, SnelKokend = false }); Potato p = new Potato() { Color = "Multicolor", Shape = "Square", Size = 2, SnelKokend = false }; potatoes.Add(p); p.Size = 1; potatoes.Remove(p); potatoes.RemoveAt(0); potatoes.AddRange(new Potato[] { new Potato(), new Potato(), new Potato() }); bool b = potatoes.Contains(p); List <Potato> blues = potatoes.FindAll(par => par.Color == "Blue"); potatoes.ForEach(item => Console.WriteLine(item)); //potatoes.ForEach(DoSomething); Console.WriteLine("*******************"); potatoes.Sort(); potatoes.ForEach(item => Console.WriteLine(item)); Console.WriteLine("*******************"); Potato third = potatoes[3]; foreach (Potato item in potatoes) { } //List<int> numbers = new List<int>() { // 1,5,2,8,5,9,5,2,6,8,0,43,7,3 //}; //numbers.Sort(); //numbers.ForEach(n => Console.WriteLine(n)); Dictionary <string, Potato> dictionary = new Dictionary <string, Potato>(); dictionary["mario"] = p; Potato potato = dictionary["mario"]; dictionary.Add("peach", new Potato()); foreach (string key in dictionary.Keys) { Console.WriteLine($"{key} {dictionary[key]}"); } }