static void Main(string[] args) { foreach (string arg in args) { string file = arg; // Attempt to accomodate for relative or absolute directories if (file[0] != '/' && file[1] != ':') { file = Directory.GetCurrentDirectory() + Path.DirectorySeparatorChar + file; } string filenameNoExt = file.Substring(0, file.LastIndexOf('.')); bool errorOccured = false; try { CogneroTest test = CogneroXMLReader.ParseFile(file); Test2Doc.CreateDocsFromTest(test, filenameNoExt); } catch (Exception ex) { Console.Error.WriteLine("\nFAILED TO CONVERT " + file + "\nEXCEPTION:\n" + ex.Message + "\n"); errorOccured = true; } finally { if (!errorOccured) { Console.WriteLine("CONVERT " + file + " OPERATION OK"); } } } }
void MainWindow_DragDrop(object sender, DragEventArgs e) { bool errorOccured = false; string[] files = (string[])e.Data.GetData(DataFormats.FileDrop); foreach (string file in files) { string filenameNoExt = file.Substring(0, file.LastIndexOf('.')); try { CogneroTest test = CogneroXMLReader.ParseFile(file); Test2Doc.CreateDocsFromTest(test, filenameNoExt); } catch (Exception ex) { MessageBox.Show("Failed to convert " + file + " to docx. Ensure it is a well-formed XML file and you have R/W permissions for the folder.\n\nException:\n" + ex.Message, "ERROR", MessageBoxButtons.OK, MessageBoxIcon.Error); errorOccured = true; } } if (!errorOccured) { ShowNonBlockingMessageBox("Operation Succeeded", "Success", MessageBoxButtons.OK, MessageBoxIcon.Information); } else { ShowNonBlockingMessageBox("Errors occured during operation. Some or all file conversions may have failed.", "WARNING", MessageBoxButtons.OK, MessageBoxIcon.Warning); } }
// This document contains the questions and only correct answers. public static void CreateSolutionsDoc(CogneroTest test, string filename) { Formatting solutionFormatting = new Formatting(); solutionFormatting.Italic = true; var doc = DocX.Create(filename + " SOLUTIONS.docx"); int quesNum = 1; foreach (CogneroQuestion ques in test.Questions) { doc.InsertParagraph(quesNum + ". " + (ques.IsTrueFalse ? "(T/F) " : "") + ques.Text); foreach (CogneroAnswer ans in ques.Answers) { if (ans.IsSolution) { doc.InsertParagraph("SOLUTION: " + ans.Text, false, solutionFormatting); } } doc.InsertParagraph(); quesNum++; } doc.Save(); }
// Questions Document contains the questions and possible answers but NOT the solutions. public static void CreateQuestionsDoc(CogneroTest test, string filename) { var doc = DocX.Create(filename + ".docx"); int quesNum = 1; foreach (CogneroQuestion ques in test.Questions) { doc.InsertParagraph(quesNum + ". " + (ques.IsTrueFalse ? "(T/F) " : "") + ques.Text); if (!ques.IsTrueFalse) { Xceed.Document.NET.List list = doc.AddList(); foreach (CogneroAnswer ans in ques.Answers) { doc.AddListItem(list, ans.Text); } doc.InsertList(list); } doc.InsertParagraph(); quesNum++; } doc.Save(); }
// Parse the XML file and create a CogneroTest object with it public static CogneroTest ParseFile(string filepath) { CogneroTest test = new CogneroTest(); XElement testXML = XElement.Load(filepath); var questions = testXML.Element("Container").Element("Questions").Elements("Question"); foreach (XElement question in questions) { CogneroQuestion ques = new CogneroQuestion(); ques.Text = ""; // From the limited data I've seen I only need to account for True/False type questions. Other question types work fine with current method. if (question.Attribute("Type").Value.Equals("True_False")) { ques.IsTrueFalse = true; } ques.Text += removeXhtmlStuff(question.Element("XHtml").Value); var answerItems = question.Element("AnswerData").Element("Answers").Elements("AnswerItem"); // Anything not T/F (i.e. multiple choice and "open-ended questions") if (!ques.IsTrueFalse) { foreach (XElement answer in answerItems) { CogneroAnswer ans = new CogneroAnswer(); ans.Text = removeXhtmlStuff(answer.Element("XHtml").Value); if (!ans.Text.Equals("")) { if (answer.Attribute("IsCorrectAnswer").Value.Equals("true")) { ans.IsSolution = true; } ques.Answers.Add(ans); } } } // T/F questions else { // Need to add a "custom" answer to the question object to handle T/F type questions CogneroAnswer ans = new CogneroAnswer(); ans.IsSolution = true; // This element always holds either a 1 or 0 (ASCII character) and indicates if the answer is true/false respectively. var answerXML = question.Element("AnswerData").Element("Answers").Element("AnswerItem").Element("AnswerPlainTextOrIndex"); if (answerXML.Value.Equals("1")) { ans.Text = "TRUE"; } else { ans.Text = "FALSE"; } ques.Answers.Add(ans); } test.Questions.Add(ques); } return(test); }
public static void CreateDocsFromTest(CogneroTest test, string filename) { CreateQuestionsDoc(test, filename); CreateSolutionsDoc(test, filename); }