Note that there are some explanatory texts on larger screens.

plurals
  1. POC# - Serialize/Deserialize or Save/Load a list of tuples
    primarykey
    data
    text
    <p>I have a list which contains a Tuple of two strings and 25.000 elements</p> <pre><code>List&lt;Tuple&lt;string, string&gt;&gt; MyList </code></pre> <p>I try to find a way to save this list and then load it because really takes time each time to construct from the beginning the <em>MyList</em>. I tried </p> <pre><code>using System; using System.IO; using System.Text; using System.Xml.Serialization; namespace CopyFiles { /// &lt;summary&gt; /// Serializer class. Load and Save classes from/to XML files. /// &lt;/summary&gt; public class XSerial { /// &lt;summary&gt; /// Load a class from a serialized XML file. /// &lt;/summary&gt; /// &lt;param name="filename"&gt;full path or path relative to the XML file&lt;/param&gt; /// &lt;param name="t"&gt;type of the class that is being retrieved (Use typeof(ClassName))&lt;/param&gt; /// &lt;returns&gt;A populated version of the class, or null on failure&lt;/returns&gt; /// &lt;exception cref="Exception"&gt;Can throw several exceptions for IO and serialization loading&lt;/exception&gt; public static T Load&lt;T&gt;(string filename) { T ob = default(T); using (Stream s = File.Open(filename, FileMode.Open)) { StreamReader sr = new StreamReader(s); ob = DeserializeObject&lt;T&gt;(sr.ReadToEnd()); s.Close(); } return ob; } /// &lt;summary&gt; /// Save an instance of a class to an XML file /// &lt;/summary&gt; /// &lt;param name="filename"&gt;Full or relative path to the file&lt;/param&gt; /// &lt;param name="cls"&gt;Class to serialize and save.&lt;/param&gt; /// &lt;param name="t"&gt;Type of the class (use: typeof(ClassName)&lt;/param&gt; /// &lt;returns&gt;True on success, False on failure&lt;/returns&gt; public static void Save&lt;T&gt;(string filename, T cls) { using (Stream s = File.Open(filename, FileMode.Create)) { using (StreamWriter sw = new StreamWriter(s)) { sw.Write(SerializeObject&lt;T&gt;(cls)); sw.Close(); s.Close(); return; } } } /// &lt;summary&gt; /// Serialize the object into an XML format /// &lt;/summary&gt; /// &lt;typeparam name="T"&gt;Type of object to serialize&lt;/typeparam&gt; /// &lt;param name="pObject"&gt;the object to serialize&lt;/param&gt; /// &lt;returns&gt;a string representing the XML version of the object&lt;/returns&gt; public static String SerializeObject&lt;T&gt;(T pObject) { MemoryStream memoryStream = new MemoryStream(); UTF8Encoding encoding = new UTF8Encoding(); XmlSerializer xs = new XmlSerializer(typeof(T)); System.Xml.XmlTextWriter xmlTextWriter = new System.Xml.XmlTextWriter(memoryStream, Encoding.UTF8); xs.Serialize(xmlTextWriter, (object)pObject); memoryStream = (MemoryStream)xmlTextWriter.BaseStream; return encoding.GetString(memoryStream.ToArray()); } /// &lt;summary&gt; /// Deserialize the object back into the object from an XML string /// &lt;/summary&gt; /// &lt;typeparam name="T"&gt;Type of the object to restore&lt;/typeparam&gt; /// &lt;param name="pXmlizedString"&gt;The string that represents the object in XML&lt;/param&gt; /// &lt;returns&gt;A new instance of the restored object&lt;/returns&gt; public static T DeserializeObject&lt;T&gt;(String pXmlizedString) { UTF8Encoding encoding = new UTF8Encoding(); XmlSerializer xs = new XmlSerializer(typeof(T)); MemoryStream memoryStream = new MemoryStream(encoding.GetBytes(pXmlizedString)); System.Xml.XmlTextWriter xmlTextWriter = new System.Xml.XmlTextWriter(memoryStream, Encoding.UTF8); return (T)xs.Deserialize(memoryStream); } } } </code></pre> <p>which is working perfect for simple List&lt;...> </p> <pre><code>class Program { static void Main(string[] args) { List&lt;string&gt; MyList = null; try { MyList = XSerial.Load&lt;List&lt;string&gt;&gt;("MyList.xml"); } catch (Exception e) { Console.WriteLine("Error: " + e.Message); // make sure the object won't cause errors. MyList = new List&lt;string&gt;(); } Console.WriteLine("List Items:"); foreach (string item in MyList) { Console.WriteLine("In list: " + item); } MyList.Add("Orange"); MyList.Add("Blue"); MyList.Add("Green"); Console.WriteLine("Saving list...\n"); try { XSerial.Save&lt;List&lt;string&gt;&gt;("MyList.xml", MyList); } catch (Exception e) { Console.WriteLine("Error Saving: " + e.Message); // nothing can be done about recovery. } Console.WriteLine("Press any key to continue..."); Console.ReadKey(); } } </code></pre> <p>but not for a list with tuple because XmlSerializer to be able to do it's job needs a default constructor. That is a constructor that takes no arguments. All the Tuple&lt;...> classes have a single constructor and that constructor takes a number of arguments.</p> <p><strong>My Question:</strong> Is there any way the above XSerial to do the job for a list of tuples or should i use something else like <a href="http://msdn.microsoft.com/en-us/library/system.runtime.serialization.formatters.binary.binaryformatter.aspx" rel="nofollow">http://msdn.microsoft.com/en-us/library/system.runtime.serialization.formatters.binary.binaryformatter.aspx</a> ? Thanks in advance.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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