SafeDispatch/SafeMobileLIB_DLL/Helpers/SerializationHelpser.cs

57 lines
1.6 KiB
C#
Raw Permalink Normal View History

2024-02-22 16:43:59 +00:00
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Xml.Linq;
using System.Xml.Serialization;
namespace SafeMobileLib.Helpers
{
public static class SerializationHelper
{
public static void Serialize(this List<string> list, string fileName)
{
var serializer = new XmlSerializer(typeof(List<string>));
using (var stream = File.OpenWrite(fileName))
{
serializer.Serialize(stream, list);
}
}
public static void Deserialize(this List<string> list, string fileName)
{
var serializer = new XmlSerializer(typeof(List<string>));
using (var stream = File.OpenRead(fileName))
{
var other = (List<string>)(serializer.Deserialize(stream));
list.Clear();
list.AddRange(other);
}
}
public static List<String> DesirializeWithLinq(string path)
{
XElement root = XElement.Load(path);
var numbers = root.Descendants("gps")
.Select(x => x.Value).ToList();
return numbers;
}
public static void SerializeWithLinq(List<string> lst, string path)
{
string name = "gps";
XElement root = new XElement("gps");
int cnt = 0;
foreach (var item in lst)
{
root.Add(new XElement(name, item.ToString()));
cnt++;
}
root.Save(path);
}
}
}