Advertisement
cuniszkiewicz

Serializacja_glowny

Mar 19th, 2025
149
0
29 days
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.41 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using System.IO;
  7. using System.Runtime.Serialization.Formatters.Binary;
  8. using System.Xml.Serialization;
  9.  
  10.  
  11. namespace SerializationXML_eng
  12. {
  13.     class Program
  14.     {
  15.         static void Main(string[] args)
  16.         {
  17.             Animal animal = new Animal("Beaver", "Brown", 2, 1, 2);
  18.             //saving one object to xml file:
  19.             XmlSerializer serializer = new XmlSerializer(typeof(Animal));
  20.  
  21.             using (TextWriter tw = new StreamWriter(@"D:\Animals.xml"))
  22.             {
  23.                 serializer.Serialize(tw, animal);
  24.             }
  25.  
  26.             //printing object
  27.             Console.WriteLine(animal.ToString());
  28.             //setting object to null
  29.             animal = null;
  30.             // reading from file:
  31.             XmlSerializer deSerializer = new XmlSerializer(typeof(Animal));
  32.             TextReader reader = new StreamReader(@"D:\Animals.xml");
  33.             object o = deSerializer.Deserialize(reader);
  34.             animal = (Animal)o;
  35.             reader.Close();
  36.             //printing object from xml file
  37.             Console.WriteLine(animal.ToString());
  38.  
  39.             //Writing list to xml
  40.  
  41.             //list of objects
  42.             List<Animal> Animals = new List<Animal>
  43.             {
  44.                 new Animal("goat","white",7,1.5,45),
  45.                 new Animal("rabbit", "gray", 3, 0.3, 0.75),
  46.                 new Animal("cockroach", "black", 1, 0.01, 0.01),
  47.             };
  48.  
  49.             //saving list
  50.             using (Stream fs = new FileStream(@"D:\AnimalsList.xml", FileMode.Create, FileAccess.Write, FileShare.None))
  51.             {
  52.                 XmlSerializer serializer2 = new XmlSerializer(typeof(List<Animal>));
  53.                 serializer2.Serialize(fs, Animals);
  54.  
  55.             }
  56.             //clearing list
  57.             Animals = null;
  58.  
  59.             //reading from xml file
  60.             XmlSerializer serializer3 = new XmlSerializer(typeof(List<Animal>));
  61.             using (FileStream fs2 = File.OpenRead(@"D:\AnimalsList.xml"))
  62.             {
  63.                 Animals = (List<Animal>)serializer3.Deserialize(fs2);
  64.             }
  65.             //printing all list
  66.             foreach (Animal a in Animals)
  67.             {
  68.                 Console.WriteLine(a.ToString());
  69.             }
  70.             Console.ReadKey();
  71.  
  72.  
  73.  
  74.         }
  75.     }
  76. }
  77.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement