SERIALIZATION
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()
{
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);
}
SERIALIZATION - GENERIC
var result2 = new List()
{
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(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;
}
FILE PATH
C:\Users\611184113\source\repos\SRP\SRP\bin\Debug\net6.0\cricket.xml
sky
T20
40.2
131.2
sky1
T20
40.5
131.2
1>> DESERIALIZATION
string path = Path.Combine(Environment.CurrentDirectory, "cricket.xml");
XmlSerializer xmlSerializer = new XmlSerializer(typeof(List));
var result1 = new List();
using (FileStream stream = File.Open(path, FileMode.Open))
{
result1 = (List)xmlSerializer.Deserialize(stream);
}
2 >> DESERIALIZATION - GENERIC
public T DeserializeToObject(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();
var ratingObject = JsonConvert.DeserializeObject>(ratingJson);