Created
March 3, 2022 08:34
-
-
Save mirmostafa/0a67037b7ef195590319d8180c1040ed to your computer and use it in GitHub Desktop.
XML Serializer
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| public static class XmlSerializer | |
| { | |
| public static void Serialize<T>(T? o, string path!!, bool indent = true) | |
| { | |
| var serializer = new System.Xml.Serialization.XmlSerializer(typeof(T)); | |
| using var writer = new StreamWriter(path); | |
| using var xmlWriter = System.Xml.XmlWriter.Create(writer, new System.Xml.XmlWriterSettings { Indent = indent }); | |
| serializer.Serialize(xmlWriter, o); | |
| } | |
| public static T? Desrialize<T>(string path!!) | |
| { | |
| using var file = new FileStream(path, FileMode.Open); | |
| var serializer = new System.Xml.Serialization.XmlSerializer(typeof(T)); | |
| var result = serializer.Deserialize(file); | |
| return result is null ? default : (T)result; | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment