Last active
December 8, 2020 09:33
-
-
Save OnSive/c9db6cc844c3f04f0b8e6a6108236ddb to your computer and use it in GitHub Desktop.
[XML de-/serialization] Convertion between XML and objects #XML #Serialize
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
| using System.IO; | |
| using System.Text; | |
| using System.Xml.Serialization; | |
| /// <summary> | |
| /// Converts an XML string to a class (XML import) | |
| /// </summary> | |
| /// <typeparam name="TReturn">The class to which the XML should be converted</typeparam> | |
| public static TReturn DeserializeXml<TReturn>(this string sXml) | |
| { | |
| // If the string is empty | |
| if (string.IsNullOrEmpty(sXml)) | |
| { | |
| // Returns the empty class | |
| return default; | |
| } | |
| // Initializes an XmlSerializer with the class | |
| XmlSerializer XmlSerializer_Klasse = new XmlSerializer(typeof(TReturn)); | |
| // Uses a StringReader with the passed value | |
| using (StringReader StringReader_Text = new StringReader(sXml)) | |
| { | |
| // Deserializes the StringReader to an object | |
| object Objekt_Deserialisiert = XmlSerializer_Klasse.Deserialize(StringReader_Text); | |
| // Returns the object as class | |
| return (TReturn)Objekt_Deserialisiert; | |
| } | |
| } |
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
| using System.IO; | |
| using System.Text; | |
| using System.Xml.Serialization; | |
| /// <summary> | |
| /// Converts a class to XML (XML export) | |
| /// </summary> | |
| public static string SerializeXml<TSerializable>(this TSerializable obj) | |
| where TSerializable : ... // Remove or enter your own serializeable interface or class | |
| { | |
| // Initializes an XmlSerializer with the given object | |
| XmlSerializer XmlSerializer_Xml = new XmlSerializer(obj.GetType()); | |
| // Uses a StringWriter to write the xml | |
| using (StringWriterUtf8 StringWriterUtf8 = new StringWriterUtf8()) | |
| { | |
| // Writes the object to xml in the StringWriter | |
| XmlSerializer_Xml.Serialize(StringWriterUtf8, obj); | |
| // Returns the object as xml in string form | |
| return StringWriterUtf8.ToString(); | |
| } | |
| } | |
| /// <summary> | |
| /// StringWriter to write the XML as UTF-8 | |
| /// </summary> | |
| private class StringWriterUtf8 : StringWriter | |
| { | |
| /// <summary> | |
| /// Overrides the encoding property to return UTF-8 as encoding. | |
| /// </summary> | |
| public override Encoding Encoding | |
| { | |
| get | |
| { | |
| // Returns UTF-8 | |
| return Encoding.UTF8; | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment