Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>I am fan of extension methods, therefore I use this always:</p> <pre><code>using System.IO; using System.Xml.Serialization; public static class SerializationExtensionMethods { /// &lt;summary&gt; /// Serializes the object. /// &lt;/summary&gt; /// &lt;typeparam name="T"&gt;&lt;/typeparam&gt; /// &lt;param name="toSerialize"&gt;To serialize.&lt;/param&gt; /// &lt;returns&gt;&lt;/returns&gt; public static string SerializeObjectToXml&lt;T&gt;(this T toSerialize) { XmlSerializer xmlSerializer = new XmlSerializer(toSerialize.GetType()); StringWriter textWriter = new StringWriter(); xmlSerializer.Serialize(textWriter, toSerialize); return textWriter.ToString(); } /// &lt;summary&gt; /// Serializes the object. /// &lt;/summary&gt; /// &lt;typeparam name="T"&gt;&lt;/typeparam&gt; /// &lt;param name="toSerialize"&gt;To serialize.&lt;/param&gt; /// &lt;param name="path"&gt;The path.&lt;/param&gt; public static void SerializeObjectToFile&lt;T&gt;(this T toSerialize, string path) { string xml = SerializeObjectToXml&lt;T&gt;(toSerialize); using (StreamWriter sw = new StreamWriter(path, false)) { sw.Write(xml); } } /// &lt;summary&gt; /// Deserializes the specified XML. /// &lt;/summary&gt; /// &lt;typeparam name="T"&gt;&lt;/typeparam&gt; /// &lt;param name="xml"&gt;The XML.&lt;/param&gt; /// &lt;returns&gt;&lt;/returns&gt; public static T DeserializeFromXml&lt;T&gt;(this T original, string xml) { XmlSerializer serializer = new XmlSerializer(typeof(T)); TextReader textReader = new StringReader(xml); return (T)serializer.Deserialize(textReader); } /// &lt;summary&gt; /// Deserializes the specified object. /// &lt;/summary&gt; /// &lt;typeparam name="T"&gt;&lt;/typeparam&gt; /// &lt;param name="original"&gt;The original.&lt;/param&gt; /// &lt;param name="path"&gt;The path.&lt;/param&gt; /// &lt;returns&gt;&lt;/returns&gt; public static T DeserializeFromFile&lt;T&gt;(this T original, string path) { string xml = string.Empty; using (StreamReader sr = new StreamReader(path)) { xml = sr.ReadToEnd(); } return DeserializeFromXml&lt;T&gt;(original, xml); } } </code></pre> <p>Usage to serialize:</p> <pre><code>YourClassType obj = new YourClassType(); </code></pre> <p>or</p> <pre><code>List&lt;YourClassType&gt; obj = new List&lt;YourClassType&gt;(); string xml = obj.SerializeObjectToXml(); </code></pre> <p>or</p> <pre><code>obj.SerializeObjectToFile("PathToYourFile"); // It will save a file with your classes serialized (works with everything with the [Serializable] attribute). </code></pre> <p>Usage to deserialize:</p> <pre><code>YourClassType obj = new YourClassType().DeserializeFromXml("XML string here"); List&lt;YourClassType&gt; obj = new List&lt;YourClassType&gt;().DeserializeFromFile("XML string here"); </code></pre> <p>or </p> <pre><code>YourClassType obj = new YourClassType().DeserializeFromFile("PathToYourFile"); </code></pre> <p>And you have it running :)</p> <p>I prefer extension methods because it allows you to have your code very clean, this works with every kind of object type you have, as far as it implements the <code>[Serializable]</code> attribute on it.</p> <p>If you need to specify how it will be serialized (as nodes or attributes), you can add the attribute on each of your properties such as:</p> <pre><code>[XmlElement("NameOfTheElementYouWant")] [XmlAttribute("NameOfTheAttributeYouWant")] [XmlText] </code></pre> <p>Hope this helps someone in the future.</p> <p>Alejandro</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload