private void Button2_Click(object sender, System.EventArgs e) { System.Runtime.Serialization.Formatters.Soap.SoapFormatter b = new System.Runtime.Serialization.Formatters.Soap.SoapFormatter(); System.IO.FileStream f = new System.IO.FileStream("aaa.xml", System.IO.FileMode.Create); b.Serialize(f, col); f.Close(); }
private static GlobalSettings GetSettings() { string settingsFilePath = SettingsFilePath; if (!System.IO.File.Exists(settingsFilePath)) { var defaultSettings = CreateDefaultSettings(); defaultSettings.SaveSettings(); return(defaultSettings); } try { System.IO.FileStream file = System.IO.File.OpenRead(settingsFilePath); System.Runtime.Serialization.Formatters.Soap.SoapFormatter soapFormatter = new System.Runtime.Serialization.Formatters.Soap.SoapFormatter(); GlobalSettings settings = soapFormatter.Deserialize(file) as GlobalSettings; AssignCameraNames(settings); return(settings); } catch (Exception) { } return(CreateDefaultSettings()); }
public void OneFormat() { BluetoothAddress obj = BluetoothAddress.Parse("001122334455"); // IFormatter szr = new System.Runtime.Serialization.Formatters.Soap.SoapFormatter(); Stream strm = new MemoryStream(); szr.Serialize(strm, obj); // SZ Format const String NewLine = "\r\n"; String xmlData = "<SOAP-ENV:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:SOAP-ENC=\"http://schemas.xmlsoap.org/soap/encoding/\" xmlns:SOAP-ENV=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:clr=\"http://schemas.microsoft.com/soap/encoding/clr/1.0\" SOAP-ENV:encodingStyle=\"http://schemas.xmlsoap.org/soap/encoding/\">" + NewLine + "<SOAP-ENV:Body>" + NewLine + "<a1:BluetoothAddress id=\"ref-1\" xmlns:a1=\"http://schemas.microsoft.com/clr/nsassem/InTheHand.Net/[A-F-N]\">" + NewLine + "<dataString id=\"ref-3\">001122334455</dataString>" + NewLine + "</a1:BluetoothAddress>" + NewLine + "</SOAP-ENV:Body>" + NewLine + "</SOAP-ENV:Envelope>" + NewLine ; strm.Position = 0; String result = new StreamReader(strm).ReadToEnd(); Assert.AreEqual(InsertAssemblyFullNameEscaped(xmlData), result, "Equals"); }
public static string Serialize(object objectToSerialize, System.Text.Encoding encoding, bool formatXml) { try { MemoryStream mem = new System.IO.MemoryStream(); System.Runtime.Serialization.Formatters.Soap.SoapFormatter formatter = new System.Runtime.Serialization.Formatters.Soap.SoapFormatter(); formatter.Serialize(mem, objectToSerialize); mem.Flush(); mem.Position = 0; byte[] data = mem.ToArray(); string xml = encoding.GetString(data, 0, data.Length); if (formatXml) { return(Utility.FormatXml(xml)); } else { return(xml); } } catch (System.Runtime.Serialization.SerializationException ex) { throw new Exception("Serialization Error: " + ex.Message, ex); } catch (Exception ex) { throw new Exception("Serialization Error: " + ex.Message, ex); } }
public void DeserializationTest() { System.IO.FileStream file = null; System.Runtime.Serialization.Formatters.Soap.SoapFormatter formatter = new System.Runtime.Serialization.Formatters.Soap.SoapFormatter(); try { string path = "session.xml"; using (file = new System.IO.FileStream(path, System.IO.FileMode.Open)) { object temp = formatter.Deserialize(file); FilmSession session = null; if (temp is FilmSession) { session = (FilmSession)temp; } else { session = new FilmSession(); session.AddFilmBox(temp as FilmBox); } print(session, new ApplicationEntity("NER_8900", IPAddress.Parse("127.0.0.1"), 2008)); } } finally { if (file != null) { file.Close(); file.Dispose(); file = null; } } }
// Sets the job tree item selections from a serialized settings string. // private void ApplyJobDefSetPrefs(string Text) { // deserialize node settings System.IO.MemoryStream stream = new System.IO.MemoryStream(); System.IO.StreamWriter sw = new System.IO.StreamWriter(stream); sw.Write(Text); sw.Flush(); System.Runtime.Serialization.Formatters.Soap.SoapFormatter formatter = new System.Runtime.Serialization.Formatters.Soap.SoapFormatter(); stream.Position = 0; System.Collections.Specialized.NameValueCollection CheckedNodes = (System.Collections.Specialized.NameValueCollection)formatter.Deserialize(stream); // apply node settings foreach (System.Windows.Forms.TreeNode Node in TreeView.Nodes) { string Value = CheckedNodes.Get(Node.FullPath); Node.Checked = CvtToChecked(Value); foreach (System.Windows.Forms.TreeNode Subnode in Node.Nodes) { Value = CheckedNodes.Get(Subnode.FullPath); Subnode.Checked = CvtToChecked(Value); } } }
/// <summary> /// Saves or Serializes a Cluster Collection To an Xml file /// </summary> /// <param name="myObject">A serializable object to be persisted to an Xml file</param> /// <param name="writeToXmlPath">The location of the Xml file tha will contain serialized data</param> /// <returns>True if the serialization is successful otherwise false</returns> public static bool Serialize(System.Object myObject, string writeToXmlPath) { bool state = true; System.Runtime.Serialization.IFormatter formatter = null; System.IO.Stream stream = null; try { formatter = new System.Runtime.Serialization.Formatters.Soap.SoapFormatter(); stream = new System.IO.FileStream(writeToXmlPath, FileMode.Create, FileAccess.Write, FileShare.None); formatter.Serialize(stream, myObject); } catch (System.Exception ex) { state = false; System.Diagnostics.Debug.WriteLine(ex.ToString()); } finally { stream.Close(); formatter = null; stream = null; } return(state); }
private void Load() { path = Path.Combine(SettingsFolder, String.Format("{0}.settings.xml", application)); this.settings = new System.Collections.Specialized.NameValueCollection(); System.IO.FileStream file = null; try { lock (sentry) { file = new System.IO.FileStream(path, System.IO.FileMode.OpenOrCreate, System.IO.FileAccess.Read); if (file.Length > 0) { System.Runtime.Serialization.Formatters.Soap.SoapFormatter formatter = new System.Runtime.Serialization.Formatters.Soap.SoapFormatter(); settings = (System.Collections.Specialized.NameValueCollection)formatter.Deserialize(file); } } } catch { } finally { if (file != null) { file.Close(); file.Dispose(); file = null; } } }
public void Serialization05Test() { MemoryStream buffer = new MemoryStream(); Sfmt.Soap.SoapFormatter formatter = new Sfmt.Soap.SoapFormatter(); formatter.AssemblyFormat = Sfmt.FormatterAssemblyStyle.Simple; // Create some call: MockingProxy callee = new MockingProxy(typeof(Sample.FooBar), null, "m1"); MockableCall call = new MockableCall(callee, typeof(Sample.FooBar).GetMethod("SomeVoid"), new object[] { }); call.SetCallResult(); // Serialize it: formatter.Serialize(buffer, call); buffer.Flush(); // Deserialize it: buffer.Position = 0; MockableCall deserializedCall = (MockableCall)formatter.Deserialize(buffer); // Test result: Assert.IsNotNull(deserializedCall); Assert.IsFalse(deserializedCall.IsConstructorCall); Assert.IsNotNull(deserializedCall.Method); Assert.AreEqual("SomeVoid", deserializedCall.Method.Name); Assert.IsTrue(deserializedCall.ReturnValue == null); Assert.IsTrue(deserializedCall.IsCompleted); }
private void Save() { System.IO.FileStream file = null; try { lock (sentry) { file = new System.IO.FileStream(path, System.IO.FileMode.Create); System.Runtime.Serialization.Formatters.Soap.SoapFormatter formatter = new System.Runtime.Serialization.Formatters.Soap.SoapFormatter(); formatter.Serialize(file, settings); } } catch { } finally { if (file != null) { file.Flush(); file.Dispose(); file = null; } } }
public static DataSet ReadFilmBox(FileStream stream, Size size) { System.Runtime.Serialization.Formatters.Soap.SoapFormatter formatter = new System.Runtime.Serialization.Formatters.Soap.SoapFormatter(); FilmBox page = (FilmBox)formatter.Deserialize(stream); return(RenderPage(page, size)); }
public void Serialization04Test() { MemoryStream buffer = new MemoryStream(); Sfmt.Soap.SoapFormatter formatter = new Sfmt.Soap.SoapFormatter(); formatter.AssemblyFormat = Sfmt.FormatterAssemblyStyle.Simple; // Create some call: MockingProxy callee = new MockingProxy(typeof(Sample.FooBar), null, "m1"); MockableCall call = new MockableCall(callee, typeof(Sample.FooBar).GetMethod("IsItTrue"), new object[] { true }); call.SetCallResult(true); // Serialize it: formatter.Serialize(buffer, call); buffer.Flush(); // Deserialize it: buffer.Position = 0; MockableCall deserializedCall = (MockableCall)formatter.Deserialize(buffer); // Test result: Assert.IsNotNull(deserializedCall); Assert.IsFalse(deserializedCall.IsConstructorCall); Assert.IsNotNull(deserializedCall.Method); Assert.AreEqual("IsItTrue", deserializedCall.Method.Name); Assert.IsFalse(deserializedCall.ReturnValue is string, "ReturnValue of type bool was read back through soap serialization as a string."); Assert.IsTrue(deserializedCall.ReturnValue is bool); Assert.IsTrue(deserializedCall.InArgs[0] is bool); Assert.IsTrue((bool)deserializedCall.ReturnValue); Assert.IsTrue(deserializedCall.IsCompleted); }
// Add cookies to WebRequest protected override WebRequest GetWebRequest(Uri address) { var request = (HttpWebRequest)base.GetWebRequest(address); request.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8"; request.KeepAlive = true; request.Method = "POST"; request.ContentType = "application/x-www-form-urlencoded"; request.UserAgent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/62.0.3202.89 Safari/537.36 OPR/49.0.2725.39"; request.AllowAutoRedirect = true; CookieContainer retrievedCookies = null; var formatter = new System.Runtime.Serialization.Formatters.Soap.SoapFormatter(); string file = "1.data"; try { using (Stream s = File.OpenRead(file)) retrievedCookies = (CookieContainer)formatter.Deserialize(s); } catch { Console.WriteLine("errors cookeis"); retrievedCookies = new CookieContainer(); } request.CookieContainer = retrievedCookies; return(request); }
//************************************************************** // Load() // This static method is the way to instantiate manifest objects //************************************************************** public static AppManifest Load(string manifestFilePath) { Stream stream = null; try { if (!File.Exists(manifestFilePath)) { return(new AppManifest(manifestFilePath)); } IFormatter formatter = new System.Runtime.Serialization.Formatters.Soap.SoapFormatter(); stream = new FileStream(manifestFilePath, FileMode.Open, FileAccess.Read, FileShare.Read); AppManifest Manifest = (AppManifest)formatter.Deserialize(stream); Manifest.FilePath = manifestFilePath; stream.Close(); return(Manifest); } catch (Exception e) { if (stream != null) { stream.Close(); } Debug.WriteLine("APPMANAGER: ERROR loading app manifest, creating a new manifest."); Debug.WriteLine("APPMANAGER: " + e.ToString()); return(new AppManifest(manifestFilePath)); } }
/// <summary> /// /// </summary> /// <param name="request"></param> /// <returns></returns> public static System.IO.MemoryStream SerializeMemoryStreamSOAP(object request) { System.Runtime.Serialization.Formatters.Soap.SoapFormatter serializer = new System.Runtime.Serialization.Formatters.Soap.SoapFormatter(); System.IO.MemoryStream memStream = new System.IO.MemoryStream(); serializer.Serialize(memStream, request); return(memStream); }
/// <summary> /// /// </summary> /// <param name="request"></param> /// <returns></returns> public static System.IO.MemoryStream SerializeMemoryStreamSOAP(object request) { System.Runtime.Serialization.Formatters.Soap.SoapFormatter serializer = new System.Runtime.Serialization.Formatters.Soap.SoapFormatter(); System.IO.MemoryStream memStream = new System.IO.MemoryStream(); serializer.Serialize(memStream, request); return memStream; }
private void OnJobPrinted(Object sender, PrintJobEventArgs args) { System.IO.FileStream file = null; System.Runtime.Serialization.Formatters.Soap.SoapFormatter formatter = new System.Runtime.Serialization.Formatters.Soap.SoapFormatter(); using (file = new System.IO.FileStream("session.xml", System.IO.FileMode.Create)) { formatter.Serialize(file, args.Session); } }
/// <summary> /// 傳入一個對像把它返回成 一個記憶體的流 /// </summary> /// <param name="objectModel">序列化的對像</param> /// <returns></returns> public System.IO.MemoryStream SerializeClassToStream(object objectModel) { //實例化操作的各對象 streamMemory = new MemoryStream(); mySerializeSOAP = new System.Runtime.Serialization.Formatters.Soap.SoapFormatter(); //開始執行操作 mySerializeSOAP.Serialize(streamMemory, objectModel); return(streamMemory); }
/// <summary> /// /// </summary> /// <param name="memStream"></param> /// <returns></returns> public static object DeSerializeMemoryStreamSOAP(System.IO.MemoryStream memStream) { object sr; System.Runtime.Serialization.Formatters.Soap.SoapFormatter deserializer = new System.Runtime.Serialization.Formatters.Soap.SoapFormatter(); memStream.Position = 0; sr = deserializer.Deserialize(memStream); memStream.Close(); return(sr); }
public static object DeSerializeSOAP(string SOAP) { if (string.IsNullOrEmpty(SOAP)) { throw new ArgumentException("SOAP can not be null/empty"); } using (System.IO.MemoryStream Stream = new System.IO.MemoryStream(UTF8Encoding.UTF8.GetBytes(SOAP))) { System.Runtime.Serialization.Formatters.Soap.SoapFormatter Formatter = new System.Runtime.Serialization.Formatters.Soap.SoapFormatter(); return(Formatter.Deserialize(Stream)); } }
/// <summary> /// 傳入一個記憶體的流把它返回成 一個對像 /// </summary> /// <param name="streamobj">返序列化的流</param> /// <returns></returns> public object SerializeStreamToClass(MemoryStream streamobj) { //用於返回反序列華的類型的結果 object Serializeobject; //實例化操作各對的對像 mySerializeSOAP = new System.Runtime.Serialization.Formatters.Soap.SoapFormatter(); Serializeobject = mySerializeSOAP.Deserialize(streamMemory); streamMemory.Close(); //關了 streamMemory.Dispose(); //清理 return(Serializeobject); }
/// <summary> /// 反序列化对象 /// </summary> /// <typeparam name="T">目标类型</typeparam> /// <param name="bytes">源数据</param> /// <returns></returns> public T Deserialize <T>(byte[] bytes) { if (bytes == null) { return(default(T)); } var format = new System.Runtime.Serialization.Formatters.Soap.SoapFormatter(); using (var st = new MemoryStream(bytes)) { return((T)format.Deserialize(st)); } }
public bool Login(string loginPageAddress, NameValueCollection loginData) { var request = (HttpWebRequest)WebRequest.Create(loginPageAddress); request.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8"; request.KeepAlive = true; request.Method = "POST"; request.ContentType = "application/x-www-form-urlencoded"; request.UserAgent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/62.0.3202.89 Safari/537.36 OPR/49.0.2725.39"; request.Referer = @"https://www.facebook.com"; request.AllowAutoRedirect = true; var parameters = new StringBuilder(); foreach (string key in loginData.Keys) { parameters.AppendFormat("{0}={1}&", HttpUtility.UrlEncode(key), HttpUtility.UrlEncode(loginData[key])); } parameters.Length -= 1; var buffer = Encoding.ASCII.GetBytes(parameters.ToString()); request.ContentLength = buffer.Length; var requestStream = request.GetRequestStream(); requestStream.Write(buffer, 0, buffer.Length); requestStream.Close(); /// var formatter = new System.Runtime.Serialization.Formatters.Soap.SoapFormatter(); string file = "1.data"; using (Stream s = File.OpenRead(file)) request.CookieContainer = (CookieContainer)formatter.Deserialize(s); //// // request.CookieContainer = new CookieContainer(); var response = (HttpWebResponse)request.GetResponse(); string src = new StreamReader(response.GetResponseStream()).ReadToEnd(); response.Close(); System.IO.File.WriteAllText("3.html", System.Net.WebUtility.HtmlDecode(src)); CookieContainer = request.CookieContainer; return(response.StatusCode == HttpStatusCode.OK); }
/// <summary> /// /// </summary> /// <param name="bytesNeedDeserialized"></param> /// <param name="count"></param> /// <param name="formatType"></param> /// <returns></returns> public static object DeserializeBytesToObject(byte[] bytesNeedDeserialized,int count,SeralizeFormatType formatType) { System.Runtime.Serialization.IFormatter oFormatter = null; if (formatType == SeralizeFormatType.BinaryFormat) oFormatter = new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter(); else if (formatType == SeralizeFormatType.XmlFortmat) oFormatter = new System.Runtime.Serialization.Formatters.Soap.SoapFormatter(); System.IO.MemoryStream oStream = new System.IO.MemoryStream(bytesNeedDeserialized,0,count); object oResult = oFormatter.Deserialize(oStream); return oResult; }
private void Button4_Click(object sender, System.EventArgs e) { System.Runtime.Serialization.Formatters.Soap.SoapFormatter b = new System.Runtime.Serialization.Formatters.Soap.SoapFormatter(); System.IO.FileStream f = new System.IO.FileStream("aaa.xml", FileMode.Open); col = ((ArrayList)(b.Deserialize(f))); Graphics g = this.CreateGraphics(); foreach (Shape x in col) { x.Draw(g); } f.Close(); }
/// <summary> /// 反序列化对象 /// </summary> /// <param name="bytes">源数据</param> /// <param name="targetType">目标类型</param> /// <returns></returns> public object Deserialize(byte[] bytes, Type targetType) { if (bytes == null) { return(null); } var format = new System.Runtime.Serialization.Formatters.Soap.SoapFormatter(); using (var st = new MemoryStream(bytes)) { return(format.Deserialize(st)); } }
/// <summary> /// 序列化对象 /// </summary> /// <param name="graph">源数据</param> /// <returns></returns> public byte[] SerializeObject(object graph) { if (graph == null) { return(null); } var format = new System.Runtime.Serialization.Formatters.Soap.SoapFormatter(); using (var st = new MemoryStream()) { format.Serialize(st, graph); return(st.ToArray()); } }
private void SaveSettings() { string settingsFilePath = SettingsFilePath; try { System.IO.Directory.CreateDirectory(System.IO.Directory.GetParent(settingsFilePath).FullName); System.IO.FileStream file = System.IO.File.OpenWrite(settingsFilePath); System.Runtime.Serialization.Formatters.Soap.SoapFormatter soapFormatter = new System.Runtime.Serialization.Formatters.Soap.SoapFormatter(); soapFormatter.Serialize(file, this); } catch (Exception) { } }
private void Save() { try { System.IO.FileStream lcFileStream = new System.IO.FileStream(fileName, System.IO.FileMode.Create); System.Runtime.Serialization.Formatters.Soap.SoapFormatter lcFormatter = new System.Runtime.Serialization.Formatters.Soap.SoapFormatter(); lcFormatter.Serialize(lcFileStream, theArtistList); lcFileStream.Close(); } catch (Exception e) { MessageBox.Show(e.Message, "File Save Error"); } }
public void OneFormat() { BluetoothEndPoint obj = new BluetoothEndPoint( BluetoothAddress.Parse("001122334455"), BluetoothService.SerialPort); // IFormatter szr = new System.Runtime.Serialization.Formatters.Soap.SoapFormatter(); Stream strm = new MemoryStream(); szr.Serialize(strm, obj); // SZ Format const String NewLine = "\r\n"; String xmlData = "<SOAP-ENV:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:SOAP-ENC=\"http://schemas.xmlsoap.org/soap/encoding/\" xmlns:SOAP-ENV=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:clr=\"http://schemas.microsoft.com/soap/encoding/clr/1.0\" SOAP-ENV:encodingStyle=\"http://schemas.xmlsoap.org/soap/encoding/\">" + NewLine + "<SOAP-ENV:Body>" + NewLine + "<a1:BluetoothEndPoint id=\"ref-1\" xmlns:a1=\"http://schemas.microsoft.com/clr/nsassem/InTheHand.Net/[A-F-N]\">" + NewLine + "<m_id href=\"#ref-3\"/>" + NewLine + "<m_service>" + NewLine + "<_a>4353</_a>" + NewLine + "<_b>0</_b>" + NewLine + "<_c>4096</_c>" + NewLine + "<_d>128</_d>" + NewLine + "<_e>0</_e>" + NewLine + "<_f>0</_f>" + NewLine + "<_g>128</_g>" + NewLine + "<_h>95</_h>" + NewLine + "<_i>155</_i>" + NewLine + "<_j>52</_j>" + NewLine + "<_k>251</_k>" + NewLine + "</m_service>" + NewLine + "<m_port>-1</m_port>" + NewLine + "</a1:BluetoothEndPoint>" + NewLine + "<a1:BluetoothAddress id=\"ref-3\" xmlns:a1=\"http://schemas.microsoft.com/clr/nsassem/InTheHand.Net/[A-F-N]\">" + NewLine + "<dataString id=\"ref-4\">001122334455</dataString>" + NewLine + "</a1:BluetoothAddress>" + NewLine + "</SOAP-ENV:Body>" + NewLine + "</SOAP-ENV:Envelope>" + NewLine ; strm.Position = 0; String result = new StreamReader(strm).ReadToEnd(); string expected = InTheHand.Net.Tests.Bluetooth.TestBluetoothAddress.IFormatterSztn .InsertAssemblyFullNameEscaped(xmlData); Assert.AreEqual(expected, result, "Equals"); }
private void Retrieve() { try { System.IO.FileStream lcFileStream = new System.IO.FileStream(fileName, System.IO.FileMode.Open); System.Runtime.Serialization.Formatters.Soap.SoapFormatter lcFormatter = new System.Runtime.Serialization.Formatters.Soap.SoapFormatter(); theArtistList = (clsArtistList)lcFormatter.Deserialize(lcFileStream); UpdateDisplay(); lcFileStream.Close(); } catch (Exception e) { MessageBox.Show(e.Message, "File Retrieve Error"); } }
private void Retrieve() { try { System.IO.FileStream lcFileStream = new System.IO.FileStream(fileName, System.IO.FileMode.Open); System.Runtime.Serialization.Formatters.Soap.SoapFormatter lcFormatter = new System.Runtime.Serialization.Formatters.Soap.SoapFormatter(); theArtistList = (clsArtistList)lcFormatter.Deserialize(lcFileStream); UpdateDisplay(); lcFileStream.Close(); } catch (Exception e) { MessageBox.Show(e.Message, "File Retrieve Error"); } }
/// <summary> /// /// </summary> /// <param name="bytesNeedDeserialized"></param> /// <param name="count"></param> /// <param name="formatType"></param> /// <returns></returns> public static object DeserializeBytesToObject(byte[] bytesNeedDeserialized, int count, SeralizeFormatType formatType) { System.Runtime.Serialization.IFormatter oFormatter = null; if (formatType == SeralizeFormatType.BinaryFormat) { oFormatter = new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter(); } else if (formatType == SeralizeFormatType.XmlFortmat) { oFormatter = new System.Runtime.Serialization.Formatters.Soap.SoapFormatter(); } System.IO.MemoryStream oStream = new System.IO.MemoryStream(bytesNeedDeserialized, 0, count); object oResult = oFormatter.Deserialize(oStream); return(oResult); }
/// <summary> /// /// </summary> /// <param name="objectNeedSerialized"></param> /// <param name="formatType"></param> /// <returns></returns> public static byte[] SerializeObjectToBytes(object objectNeedSerialized,SeralizeFormatType formatType) { System.Runtime.Serialization.IFormatter oFormatter = null; if (formatType == SeralizeFormatType.BinaryFormat) oFormatter = new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter(); else if (formatType == SeralizeFormatType.XmlFortmat) oFormatter = new System.Runtime.Serialization.Formatters.Soap.SoapFormatter(); System.IO.MemoryStream oStream = new System.IO.MemoryStream(); oFormatter.Serialize(oStream, objectNeedSerialized); byte[] oBuffer = new byte[oStream.Length]; oStream.Position = 0; oStream.Read(oBuffer, 0, oBuffer.Length); return oBuffer; }
/// <summary> /// Saves or Serializes a Cluster Collection To an Xml file /// </summary> /// <param name="myObject">A serializable object to be persisted to an Xml file</param> /// <param name="writeToXmlPath">The location of the Xml file tha will contain serialized data</param> /// <returns>True if the serialization is successful otherwise false</returns> public static bool Serialize(System.Object myObject, string writeToXmlPath) { bool state = true; System.Runtime.Serialization.IFormatter formatter = null; System.IO.Stream stream = null; try { formatter = new System.Runtime.Serialization.Formatters.Soap.SoapFormatter(); stream = new System.IO.FileStream(writeToXmlPath, FileMode.Create, FileAccess.Write, FileShare.None); formatter.Serialize(stream, myObject); } catch(System.Exception ex) { state = false; System.Diagnostics.Debug.WriteLine(ex.ToString()); } finally { stream.Close(); formatter = null; stream = null; } return state; }
private static GlobalSettings GetSettings() { string settingsFilePath = SettingsFilePath; if (!System.IO.File.Exists(settingsFilePath)) { var defaultSettings = CreateDefaultSettings(); defaultSettings.SaveSettings(); return defaultSettings; } try { System.IO.FileStream file = System.IO.File.OpenRead(settingsFilePath); System.Runtime.Serialization.Formatters.Soap.SoapFormatter soapFormatter = new System.Runtime.Serialization.Formatters.Soap.SoapFormatter(); GlobalSettings settings = soapFormatter.Deserialize(file) as GlobalSettings; AssignCameraNames(settings); return settings; } catch (Exception) { } return CreateDefaultSettings(); }
//************************************************************** // Load() // This static method is the way to instantiate manifest objects //************************************************************** public static AppManifest Load(string manifestFilePath) { Stream stream = null; try { if (!File.Exists(manifestFilePath)) return new AppManifest(manifestFilePath); IFormatter formatter = new System.Runtime.Serialization.Formatters.Soap.SoapFormatter(); stream = new FileStream(manifestFilePath, FileMode.Open, FileAccess.Read, FileShare.Read); AppManifest Manifest = (AppManifest) formatter.Deserialize(stream); Manifest.FilePath = manifestFilePath; stream.Close(); return Manifest; } catch (Exception e) { if (stream != null) stream.Close(); Debug.WriteLine("APPMANAGER: ERROR loading app manifest, creating a new manifest."); Debug.WriteLine("APPMANAGER: " + e.ToString()); return new AppManifest(manifestFilePath); } }
//************************************************************** // Update() //************************************************************** public void Update() { Stream stream = null; try { IFormatter formatter = new System.Runtime.Serialization.Formatters.Soap.SoapFormatter(); stream = new FileStream(FilePath, FileMode.Create, FileAccess.Write, FileShare.None); formatter.Serialize(stream, this); stream.Close(); } catch (Exception e) { if (stream != null) stream.Close(); Debug.WriteLine("APPMANAGER: Error saving app manfiest. " + e.Message); //throw e; } }
private void Save() { try { System.IO.FileStream lcFileStream = new System.IO.FileStream(fileName, System.IO.FileMode.Create); System.Runtime.Serialization.Formatters.Soap.SoapFormatter lcFormatter = new System.Runtime.Serialization.Formatters.Soap.SoapFormatter(); lcFormatter.Serialize(lcFileStream, theArtistList); lcFileStream.Close(); } catch (Exception e) { MessageBox.Show(e.Message, "File Save Error"); } }
private void SaveSettings() { string settingsFilePath = SettingsFilePath; try { System.IO.Directory.CreateDirectory(System.IO.Directory.GetParent(settingsFilePath).FullName); System.IO.FileStream file = System.IO.File.OpenWrite(settingsFilePath); System.Runtime.Serialization.Formatters.Soap.SoapFormatter soapFormatter = new System.Runtime.Serialization.Formatters.Soap.SoapFormatter(); soapFormatter.Serialize(file, this); } catch (Exception) { } }
/// <summary> /// /// </summary> /// <param name="memStream"></param> /// <returns></returns> public static object DeSerializeMemoryStreamSOAP(System.IO.MemoryStream memStream) { object sr; System.Runtime.Serialization.Formatters.Soap.SoapFormatter deserializer = new System.Runtime.Serialization.Formatters.Soap.SoapFormatter(); memStream.Position = 0; sr = deserializer.Deserialize(memStream); memStream.Close(); return sr; }
public void Serialization04Test() { MemoryStream buffer = new MemoryStream(); Sfmt.Soap.SoapFormatter formatter = new Sfmt.Soap.SoapFormatter(); formatter.AssemblyFormat = Sfmt.FormatterAssemblyStyle.Simple; // Create some call: MockingProxy callee = new MockingProxy(typeof(Sample.FooBar), null, "m1"); MockableCall call = new MockableCall(callee, typeof(Sample.FooBar).GetMethod("IsItTrue"), new object[] { true }); call.SetCallResult(true); // Serialize it: formatter.Serialize(buffer, call); buffer.Flush(); // Deserialize it: buffer.Position = 0; MockableCall deserializedCall = (MockableCall)formatter.Deserialize(buffer); // Test result: Assert.IsNotNull(deserializedCall); Assert.IsFalse(deserializedCall.IsConstructorCall); Assert.IsNotNull(deserializedCall.Method); Assert.AreEqual("IsItTrue", deserializedCall.Method.Name); Assert.IsFalse(deserializedCall.ReturnValue is string, "ReturnValue of type bool was read back through soap serialization as a string."); Assert.IsTrue(deserializedCall.ReturnValue is bool); Assert.IsTrue(deserializedCall.InArgs[0] is bool); Assert.IsTrue((bool)deserializedCall.ReturnValue); Assert.IsTrue(deserializedCall.IsCompleted); }
public void Serialization05Test() { MemoryStream buffer = new MemoryStream(); Sfmt.Soap.SoapFormatter formatter = new Sfmt.Soap.SoapFormatter(); formatter.AssemblyFormat = Sfmt.FormatterAssemblyStyle.Simple; // Create some call: MockingProxy callee = new MockingProxy(typeof(Sample.FooBar), null, "m1"); MockableCall call = new MockableCall(callee, typeof(Sample.FooBar).GetMethod("SomeVoid"), new object[] { }); call.SetCallResult(); // Serialize it: formatter.Serialize(buffer, call); buffer.Flush(); // Deserialize it: buffer.Position = 0; MockableCall deserializedCall = (MockableCall)formatter.Deserialize(buffer); // Test result: Assert.IsNotNull(deserializedCall); Assert.IsFalse(deserializedCall.IsConstructorCall); Assert.IsNotNull(deserializedCall.Method); Assert.AreEqual("SomeVoid", deserializedCall.Method.Name); Assert.IsTrue(deserializedCall.ReturnValue == null); Assert.IsTrue(deserializedCall.IsCompleted); }
public void Serialization03Test() { // Create and initialize formatter: Sfmt.Soap.SoapFormatter formatter = new Sfmt.Soap.SoapFormatter(); formatter.AssemblyFormat = Sfmt.FormatterAssemblyStyle.Simple; // Create DS: global::System.Data.DataSet ds = new global::System.Data.DataSet(); global::System.Data.DataTable dt = ds.Tables.Add("SomeTable"); global::System.Data.DataColumn dc1 = dt.Columns.Add("ID", typeof(global::System.Int32)); ds.AcceptChanges(); // Create MockableCall: MockingProxy callee = new MockingProxy(typeof(Sample.FooBar), null, "m1"); MockableCall call = new MockableCall(callee, typeof(Sample.FooBar).GetMethod("SomeMethodWithInsAndOuts"), new object[] { 1, 2, null, 3 }); // Set dataset as callresult: call.SetCallResult(ds); Assert.IsNotNull(call.ReturnValue, "Test setup failure, test could not even be run !"); // Serialize call: MemoryStream buffer = new MemoryStream(); formatter.Serialize(buffer, call); // Reset buffer: buffer.Flush(); buffer.Position = 0; // Deserialize call: call = (MockableCall)formatter.Deserialize(buffer); // Verify results (expect returnValue to be non-null): Assert.IsNotNull(call); Assert.IsNotNull(call.ReturnValue, "ReturnValue is null, the old implementation issue has reoccured..."); Assert.IsTrue(call.ReturnValue is global::System.Data.DataSet, "What the heck ? returnValue should have been a dataset !"); }