public CardsFileWriter(CardsFile cFile) { if (!isCardFileValid(cFile)) { throw new ArgumentException("CardsFile object is invalid."); } else { this._cFile = cFile; } }
private void btnSaveAs_Click(object sender, EventArgs e) { CardsFile cFile = null; if (saveFileDialog1.ShowDialog() == DialogResult.OK) { cFile = new CardsFile(saveFileDialog1.FileName); } try { CardsFileWriter writer = new CardsFileWriter(cFile); writer.WriteData(); } catch (ArgumentException) { MessageBox.Show("Deck data is corrupted or invalid, could not save.", "Invalid or Corrupted Project Data", MessageBoxButtons.OK, MessageBoxIcon.Error); } }
private void btnLoad_Click(object sender, EventArgs e) { try { if (openFileDialog1.ShowDialog() == DialogResult.OK) { CardsFileReader reader = new CardsFileReader(openFileDialog1.FileName); // Read cards data from the file CardsFile cFile = reader.ReadData(); // Populate cards data into UI this.deckName = cFile.DeckName; this.cards = cFile.Cards; } } catch (ArgumentException) { MessageBox.Show( "The file you have selected is not valid.", "Invalid File", MessageBoxButtons.OK, MessageBoxIcon.Error); } }
/// <summary> /// Determines if a specified <see cref="CardsFile"/> is valid. /// </summary> /// <param name="cFile">The object to check.</param> /// <returns>True if the CardsFile object can be commited to a file, otherwise False.</returns> private bool isCardFileValid(CardsFile cFile) { bool isValid = true; // Check that CardsFile isn't null if (cFile == null) { return(false); } // Check that FileLocation isn't null if (cFile.FileLocation == null || cFile.FileLocation.Length < 1) { isValid = false; } // Check that CardsFile has project name if (cFile.DeckName == null || cFile.DeckName.Length > 0) { isValid = false; } return(isValid); }
/// <summary> /// Reads card data from the XML formated file specified by the file location supplied to this class's constructor. /// </summary> /// <returns>A <see cref="CardsFile"/> object populated with the data from the file.</returns> public CardsFile ReadData() { CardsFile cFile = new CardsFile(this._fileLoc); List <CardData> cards = new List <CardData>(); XmlDocument doc = new XmlDocument(); doc.Load(this._fileLoc); // Get Deck Name and populate in cFile variable cFile.DeckName = doc.SelectSingleNode("/Cards").Attributes["DeckName"].Value; // Read each Card in the xml file and store in cards variable foreach (XmlNode node in doc.SelectNodes("/Cards/Card")) { CardData card = new CardData(); card.Question = node.SelectSingleNode("Question").InnerText; card.Answer = node.SelectSingleNode("Answer").InnerText; cards.Add(card); } // Populate cards data in cFile variable cFile.Cards = cards.ToArray(); return(cFile); }