private void btnSave_Click(object sender, EventArgs e) { Entry entry = new Entry(); entry.title = txtTitle.Text; entry.description = txtDescription.Text; entry.recommendation = txtRecommendation.Text; entry.category = cboCategory.SelectedValue.ToString(); if (entry.verifyText()) { Guid guid = (Guid)cboGuid.SelectedValue; if(guid == new Guid(Config.zeroGuid)) { guid = FolderHandler.createGuidFolder(); } entry.guid = guid; entry.langFolder = FolderHandler.createLangFolder(chkEnglish.Checked, guid); XMLHandler.saveXML(entry); updateListEntries(); } else { MessageBox.Show("Be sure to fill in all fields", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); } }
private static bool writeEntry(string path, Entry entry) { try { XmlWriterSettings settings = new XmlWriterSettings(); settings.Indent = true; settings.IndentChars = "\t"; XmlWriter writer = XmlWriter.Create(path, settings); writer.WriteStartDocument(); writer.WriteStartElement("entry"); writer.WriteStartElement("title"); writer.WriteString(entry.title); writer.WriteEndElement(); writer.WriteStartElement("descripton"); writer.WriteString(entry.description); writer.WriteEndElement(); writer.WriteStartElement("recommendation"); writer.WriteString(entry.recommendation); writer.WriteEndElement(); writer.WriteStartElement("category"); writer.WriteString(entry.category); writer.WriteEndElement(); writer.WriteEndDocument(); writer.Close(); } catch (Exception e) { return false; } return true; }
//Take all the form values and save them in the correct folder as entry.xml //data should be sent in an object called FormData //A GUID can also be selected in case one is writing for an entry that already exists //in another language (Like if sv exists and you are writing en, then you can pick //an existing GUID instead of generating a new one) public static bool saveXML(Entry entry) { String path = Path.Combine(Config.dataFolder, entry.guid.ToString(), entry.langFolder, Config.vulnFileName); DialogResult overwriteEntryAnswer; //if entry.xml exists, ask first before overwrite if + File.Exists(path) if (File.Exists(path)) { overwriteEntryAnswer = overwriteEntry(); if (overwriteEntryAnswer == DialogResult.Yes) { writeEntry(path, entry); return true; } } else { writeEntry(path, entry); return true; } return false; }