Serialization vs Deserialization(quick code)

				
					public class CricketRating
{
    public string Name { get; set; }
    public string Format { get; set; }
    public decimal Average { get; set; }
    public decimal StrikeRate { get; set; }

}
				
			
				
					string path = Path.Combine(Environment.CurrentDirectory, "cricket.xml");

        var result2 = new List<CricketRating>()
        {
            new CricketRating()
            {
                Average=46.2m,
                Format="T20",
                Name="sky",
                StrikeRate=184.2m
            },
             new CricketRating()
            {
                Average=37.75m,
                Format="T20",
                Name="KL Rahul",
                StrikeRate=140m
            }
        };

        using (FileStream stream = File.Create(path))
        {
            xmlSerializer.Serialize(stream, result2);
        }
				
			
				
					var result2 = new List<CricketRating>()
        {
            new CricketRating()
            {
                Average=46.2m,
                Format="T20",
                Name="sky",
                StrikeRate=184.2m
            },
             new CricketRating()
            {
                Average=37.78m,
                Format="T20",
                Name="KL Rahul",
                StrikeRate=140m
            }
        };
        
SerializeXml(path, result2);
				
			
				
					
    public T SerializeXml<T>(string filepath, T result) where T : class
    {
        XmlSerializer xmlSerializer = new XmlSerializer(typeof(T));

        using (FileStream stream = File.Create(filepath))
        {
            xmlSerializer.Serialize(stream, result);
        }
        return result;
    }
				
			
				
					C:\Users\611184113\source\repos\SRP\SRP\bin\Debug\net6.0\cricket.xml
				
			
				
					<ArrayOfCricketRating xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
	<CricketRating>
		<Name>sky</Name>
		<Format>T20</Format>
		<Average>40.2</Average>
		<StrikeRate>131.2</StrikeRate>
	</CricketRating>
	<CricketRating>
		<Name>sky1</Name>
		<Format>T20</Format>
		<Average>40.5</Average>
		<StrikeRate>131.2</StrikeRate>
	</CricketRating>
</ArrayOfCricketRating>
				
			
				
					     string path = Path.Combine(Environment.CurrentDirectory, "cricket.xml");
        
        XmlSerializer xmlSerializer = new XmlSerializer(typeof(List<CricketRating>));
        var result1 = new List<CricketRating>();
        using (FileStream stream = File.Open(path, FileMode.Open))
        {
            result1 = (List<CricketRating>)xmlSerializer.Deserialize(stream);
        }
				
			
				
					public T DeserializeToObject<T>(string filepath) where T : class
    {
        XmlSerializer ser = new XmlSerializer(typeof(T));

        using (StreamReader sr = new StreamReader(filepath))
        {
            return (T)ser.Deserialize(sr);
        }
    }
				
			
				
					        string path = Path.Combine(Environment.CurrentDirectory, "cricket.xml");
        var ratingObjectXml = new List<CricketRating>();
        var ratingObject = JsonConvert.DeserializeObject<List<CricketRating>>(ratingJson);


				
			

Both the result are same for 1 and 2

Leave a Comment