public void SerializeDeserializeOne()
        {
            ScriptParameterCollection collection = new ScriptParameterCollection();

            collection.Add(new ScriptParameter()
            {
                Name = "A"
            });
            collection.Add(new ScriptParameter()
            {
                Name = "B"
            });
            ScriptParameterCollection collectionClone;

            XmlSerializer serializer = new XmlSerializer(typeof(ScriptParameterCollection));

            using (Stream stream = new MemoryStream()) {
                serializer.Serialize(stream, collection);

                stream.Seek(0, SeekOrigin.Begin);
                object clone = serializer.Deserialize(stream);
                collectionClone = (ScriptParameterCollection)clone;
            }
            Assert.AreEqual(collection.Count, collectionClone.Count);
        }
Example #2
0
 /// <summary>
 /// Try to load the collection of pramters. If that fails try to load a single parameter set (backward compatibility:
 /// </summary>
 /// <param name="csqlParameterPath">The parameter file path.</param>
 /// <returns></returns>
 private static ScriptParameterCollection LoadScriptParameterFromFile(string parameterPath)
 {
     try {
         using (Stream stream = new FileStream(parameterPath, FileMode.Open, FileAccess.Read, FileShare.Read, 4096, FileOptions.SequentialScan)) {
             XmlSerializer             serializer = new XmlSerializer(typeof(ScriptParameterCollection));
             ScriptParameterCollection parameters = (ScriptParameterCollection)serializer.Deserialize(stream);
             stream.Close();
             return(parameters);
         }
     }
     catch (Exception ex) {
         string context = MethodInfo.GetCurrentMethod().Name;
         Debug.WriteLine(String.Format("{0}: Failed to load parameter because [{1}].", context, ex.Message));
     }
     try {
         using (Stream stream = new FileStream(parameterPath, FileMode.Open, FileAccess.Read, FileShare.Read, 4096, FileOptions.SequentialScan)) {
             XmlSerializer   serializer = new XmlSerializer(typeof(ScriptParameter));
             ScriptParameter parameter  = (ScriptParameter)serializer.Deserialize(stream);
             stream.Close();
             ScriptParameterCollection parameters = new ScriptParameterCollection();
             parameters.Add(parameter);
             return(parameters);
         }
     }
     catch (Exception ex) {
         string context = MethodInfo.GetCurrentMethod().Name;
         Debug.WriteLine(String.Format("{0}: Failed to load parameter because [{1}].", context, ex.Message));
     }
     return(null);
 }