/// <summary> /// importToolStripMenuItem click event handler /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void ImportToolStripMenuItem_Click(object sender, EventArgs e) { if (OfdOpenCata.ShowDialog() != System.Windows.Forms.DialogResult.OK) { return; } try { FileController fc = new FileController(); ProductCatalogueList cataList = fc.ImportCatalogues(OfdOpenCata.FileName); //Import xml data into ProductCatalogue list object. this.catalogueList.Clear(); this.catalogueList.AddRange(cataList.CatalogueList); this.InitializeCatalogueListBox(); this.LboxCatalogList.SelectedIndex = 0; //confirmation message MessageBox.Show(AppResources.MSG_INFO_FILE_IMPORTED, AppResources.MSG_CAP_INFO, MessageBoxButtons.OK, MessageBoxIcon.Information); } catch (IOException ioe) { MessageBox.Show(AppResources.EXP_MSG_IO_EXP, AppResources.MSG_CAP_ERROR, MessageBoxButtons.OK, MessageBoxIcon.Error); } catch (Exception exp) { MessageBox.Show(AppResources.EXP_MSG_EXP, AppResources.MSG_CAP_ERROR, MessageBoxButtons.OK, MessageBoxIcon.Error); } }
/// <summary> /// Import catalogues from a file. /// </summary> /// <param name="fileName"></param> /// <returns></returns> public ProductCatalogueList ImportCatalogues(string fileName) { try { //Read .xml file into the stream FileStream stream = new FileStream(fileName, FileMode.Open); XmlSerializer serializer = new XmlSerializer(typeof(ProductCatalogueList)); ProductCatalogueList cataList = (ProductCatalogueList)serializer.Deserialize(stream); stream.Close(); stream = null; return(cataList); } catch (Exception e) { throw e; } }
/// <summary> /// Export catalogues to a file. /// </summary> /// <param name="fileName"></param> /// <param name="catalogueList"></param> public bool ExportCatalogues(string fileName, List <ProductCatalogue> catalogueList) { bool ret = false; try { //Create stream to output a .xml file FileStream stream = new FileStream(fileName, FileMode.Create); StreamWriter writer = new StreamWriter(stream, Encoding.UTF8); XmlSerializerNamespaces ns = new XmlSerializerNamespaces(); ns.Add(string.Empty, string.Empty); //Xml serializer XmlSerializer serializer = new XmlSerializer(typeof(ProductCatalogueList)); //Copy the catalogue list to output ProductCatalogueList cataList = new ProductCatalogueList(); cataList.CatalogueList = new List <ProductCatalogue>(); cataList.CatalogueList.AddRange(catalogueList); serializer.Serialize(stream, cataList); stream.Flush(); stream.Close(); stream = null; ret = true; } catch (IOException ioe) { MessageBox.Show(AppResources.EXP_MSG_IO_EXP, AppResources.MSG_CAP_ERROR, MessageBoxButtons.OK, MessageBoxIcon.Error); } catch (Exception e) { MessageBox.Show(AppResources.EXP_MSG_EXP, AppResources.MSG_CAP_ERROR, MessageBoxButtons.OK, MessageBoxIcon.Error); } return(ret); }