public static LicenseXML Deserialize(string sXMLfile) { XmlSerializer xs = new XmlSerializer(typeof(LicenseXML)); StreamReader sr = new StreamReader(sXMLfile); LicenseXML lic = (LicenseXML)xs.Deserialize(sr); sr.Close(); return(lic); }
public static LicenseXML Deserialize(Stream stream) { XmlSerializer xs = new XmlSerializer(typeof(LicenseXML)); LicenseXML lic =new LicenseXML(); try{ stream.Position = 0; //rewind to start lic=(LicenseXML)xs.Deserialize(stream); }catch(XmlException ex){ System.Diagnostics.Debug.WriteLine("xml ex:" +ex.Message); } catch (Exception ex) { System.Diagnostics.Debug.WriteLine("xml ex:" + ex.Message); } return lic; }
public static string runTest() { StringBuilder sb = new StringBuilder(); //test deserialisation of license xml file sb.Append("\r\n### test deserialisation of license xml file ###\r\n"); string testFile = utils.helpers.getAppPath() + "LicenseXMLFileSample.xml"; LicenseXML licensexml = LicenseXML.Deserialize(utils.helpers.getAppPath() + "LicenseXMLFileSample.xml"); sb.Append("\r\n### testing xml file read ###\r\n"); foreach (license l in licensexml.licenses) { sb.Append(l.dumpData()); } //test deserialization of memory stream sb.Append("\r\n### testing deserialization of xml stream ###\r\n"); System.IO.MemoryStream ms = new System.IO.MemoryStream(); //read file into memorystream using (System.IO.FileStream file = new System.IO.FileStream(testFile, System.IO.FileMode.Open, System.IO.FileAccess.Read)) { byte[] bytes = new byte[file.Length]; file.Read(bytes, 0, (int)file.Length); ms.Write(bytes, 0, (int)file.Length); ms.Flush(); ms.WriteTo(new System.IO.FileStream(testFile + "_cpy", System.IO.FileMode.Create, System.IO.FileAccess.ReadWrite)); sb.Append("\r\nmemory stream copy saved to" + testFile + "_out" + "\r\n"); } LicenseXML xmlStreamed = LicenseXML.Deserialize(ms); if (xmlStreamed != null) { foreach (license l in xmlStreamed.licenses) { sb.Append(l.dumpData()); } //now test serialization of license data LicenseXML.serialize(xmlStreamed, testFile + "_out"); sb.Append("\r\ndeserilized stream object serialization test saved to" + testFile + "_out" + "\r\n"); } else { sb.Append("\r\nstream read FAILED"); } return(sb.ToString()); }
public static LicenseXML Deserialize(Stream stream) { XmlSerializer xs = new XmlSerializer(typeof(LicenseXML)); LicenseXML lic = new LicenseXML(); try{ stream.Position = 0; //rewind to start lic = (LicenseXML)xs.Deserialize(stream); }catch (XmlException ex) { System.Diagnostics.Debug.WriteLine("xml ex:" + ex.Message); } catch (Exception ex) { System.Diagnostics.Debug.WriteLine("xml ex:" + ex.Message); } return(lic); }
public static LicenseXML Deserialize(byte[] bytes) { XmlSerializer xs = new XmlSerializer(typeof(LicenseXML)); LicenseXML lic = new LicenseXML(); try { System.IO.MemoryStream ms = new MemoryStream(bytes); ms.Position = 0; //rewind to start lic = (LicenseXML)xs.Deserialize(ms); } catch (XmlException ex) { System.Diagnostics.Debug.WriteLine("xml ex:" + ex.Message); } catch (Exception ex) { System.Diagnostics.Debug.WriteLine("xml ex:" + ex.Message); } return lic; }
public static bool serialize(LicenseXML licensexml, string sXMLfile) { try { XmlSerializer xs = new XmlSerializer(typeof(LicenseXML)); //omit xmlns:xsi from xml output //Create our own namespaces for the output XmlSerializerNamespaces ns = new XmlSerializerNamespaces(); //Add an empty namespace and empty value ns.Add("", ""); StreamWriter sw = new StreamWriter(sXMLfile); xs.Serialize(sw, licensexml, ns); return(true); } catch (Exception ex) { utils.helpers.addExceptionLog(ex.Message); return(false); } }
public static LicenseXML Deserialize(byte[] bytes) { XmlSerializer xs = new XmlSerializer(typeof(LicenseXML)); LicenseXML lic = new LicenseXML(); try { System.IO.MemoryStream ms = new MemoryStream(bytes); ms.Position = 0; //rewind to start lic = (LicenseXML)xs.Deserialize(ms); } catch (XmlException ex) { System.Diagnostics.Debug.WriteLine("xml ex:" + ex.Message); } catch (Exception ex) { System.Diagnostics.Debug.WriteLine("xml ex:" + ex.Message); } return(lic); }
public int processAttachement(Attachement att, LicenseMailBodyData data, IMailMessage mail) { int iCount = 0; LicenseXML xmlData = LicenseXML.Deserialize(att.data); foreach (license ldata in xmlData.licenses) { utils.helpers.addLog("processAttachement: new LicenseData...\r\n"); LicenseData licenseData = new LicenseData(ldata.id, ldata.user, ldata.key, data.OrderNumber, data.OrderDate, data.yourPOnumber, data.EndCustomer, data.Product, data.Quantity, mail.User, mail.timestamp); //if (_licenseDataBase.addQueued(licenseData)) utils.helpers.addLog("firing license_mail event\r\n"); OnStateChanged(new StatusEventArgs(StatusType.license_mail, licenseData)); iCount++; //if (_licenseDataBase.add(ldata.id, ldata.user, ldata.key, data.OrderNumber, data.OrderDate, data.yourPOnumber, data.EndCustomer, data.Product, data.Quantity, mail.User, mail.timestamp)) // iCount++; //utils.helpers.addLog("start _licenseDataBase.add() done\r\n"); } #region alternative_code /* * // Request all the attachments on the email message. This results in a GetItem operation call to EWS. * m.Load(new Microsoft.Exchange.WebServices.Data.PropertySet(Microsoft.Exchange.WebServices.Data.EmailMessageSchema.Attachments)); * foreach (Microsoft.Exchange.WebServices.Data.Attachment att in m.Attachments) * { * if (att is Microsoft.Exchange.WebServices.Data.FileAttachment) * { * Microsoft.Exchange.WebServices.Data.FileAttachment fileAttachment = att as Microsoft.Exchange.WebServices.Data.FileAttachment; * * //get a temp file name * string fname = System.IO.Path.GetTempFileName(); //utils.helpers.getAppPath() + fileAttachment.Id.ToString() + "_" + fileAttachment.Name * * // Load the file attachment into memory. This gives you access to the attachment content, which * // is a byte array that you can use to attach this file to another item. This results in a GetAttachment operation * // call to EWS. * fileAttachment.Load(); * * // Load attachment contents into a file. This results in a GetAttachment operation call to EWS. * fileAttachment.Load(fname); * addLog("Attachement file saved to: " + fname); * * // Put attachment contents into a stream. * using (System.IO.FileStream theStream = * new System.IO.FileStream(utils.helpers.getAppPath() + fileAttachment.Id.ToString() + "_" + fileAttachment.Name, System.IO.FileMode.OpenOrCreate, System.IO.FileAccess.ReadWrite)) * { * //This results in a GetAttachment operation call to EWS. * fileAttachment.Load(theStream); * } * * //load into memory stream, seems the only stream supported * using (System.IO.MemoryStream ms = new System.IO.MemoryStream(att.Size)) * { * fileAttachment.Load(ms); * using (System.IO.FileStream fs = new System.IO.FileStream(fname, System.IO.FileMode.OpenOrCreate, System.IO.FileAccess.ReadWrite)) * { * ms.CopyTo(fs); * fs.Flush(); * } * } * addLog("saved attachement: " + fname); * iRet++; * } * } */ #endregion return(iCount); }
private void btnExport_Click(object sender, EventArgs e) { DataGridViewSelectedRowCollection rows = dataGridView1.SelectedRows; if (rows.Count > 0) { SaveFileDialog sfd = new SaveFileDialog(); sfd.DefaultExt = "xml"; sfd.CheckPathExists = true; sfd.Filter = "*.xml|xml files|*.*|all files"; sfd.FileName = "license.xml"; sfd.FilterIndex = 0; sfd.OverwritePrompt = true; sfd.RestoreDirectory = true; sfd.Title = "Enter name of file to export these " + rows.Count.ToString() + " license(s)"; sfd.ValidateNames = true; string sFileName = ""; if (sfd.ShowDialog() == System.Windows.Forms.DialogResult.OK) { sFileName = sfd.FileName; } else { return; } LicenseXML licenseXML = new LicenseXML(); List<Helpers.license> licenseList = new List<license>(); foreach (DataGridViewRow drv in rows) { DataRow dr = (drv.DataBoundItem as DataRowView).Row; Helpers.license lic = new license(); lic.id = dr["deviceid"].ToString(); lic.key = dr["key"].ToString(); lic.user = dr["endcustomer"].ToString(); licenseList.Add(lic); } licenseXML.licenses = licenseList.ToArray(); if (LicenseXML.serialize(licenseXML, sFileName)) { MessageBox.Show("license(s) saved to " + sFileName); } } }
public static bool serialize(LicenseXML licensexml, string sXMLfile) { try { XmlSerializer xs = new XmlSerializer(typeof(LicenseXML)); //omit xmlns:xsi from xml output //Create our own namespaces for the output XmlSerializerNamespaces ns = new XmlSerializerNamespaces(); //Add an empty namespace and empty value ns.Add("", ""); StreamWriter sw = new StreamWriter(sXMLfile); xs.Serialize(sw, licensexml, ns); return true; } catch (Exception ex) { utils.helpers.addExceptionLog(ex.Message); return false; } }