Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

I need an xml expert. I have an assignment to create an automatic grading system

ID: 3758980 • Letter: I

Question

I need an xml expert. I have an assignment to create an automatic grading system using xml. I have a test xml and answer key xml. I need to write code to load answer template to a windows form. Below is the code so far. The code I need help on is in bold. Thanks for suggestions

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Xml;
using System.Xml.XPath;

namespace Assignment2
{
public partial class Form1 : Form
{
private XmlDocument xmlAnswerKey;
private XmlDocument xmlAnswers;
public Form1()
{
InitializeComponent();
}

private void cbLoadAnswer_Click(object sender, EventArgs e)
{
OpenFileDialog openFileDialog1 = new OpenFileDialog();
openFileDialog1.Filter = "XML Files|*.xml";
openFileDialog1.Title = "Select a Test to Grade";

if (openFileDialog1.ShowDialog() != DialogResult.OK)
{
MessageBox.Show("You have cancelled loading of the Test. Please select another file to continue.");
goto ExitEarly;
}

xmlAnswers = new XmlDocument();
XPathNavigator xNav = xmlAnswers.CreateNavigator();
try
{
xmlAnswers.Load(openFileDialog1.FileName);
}
catch (XmlException error)
{
MessageBox.Show("Issue opening the file " + openFileDialog1.FileName + " . Error: " + error.ToString());
goto ExitEarly;
}


XmlNode root = xmlAnswers.DocumentElement;
XmlNodeList nodeList = null;

try
{
nodeList = root.SelectNodes("//answer");
}
catch (XPathException error)
{
MessageBox.Show("Sorry! There was an error locating the answers in this exam. Error: " + error.ToString());
goto ExitEarly;
}

var sw = new System.IO.StringWriter();
var xw = new System.Xml.XmlTextWriter(sw);

string AnswersbyQuestion = string.Empty;
foreach (XmlNode item in nodeList)
{
AnswersbyQuestion = AnswersbyQuestion + item.ParentNode.Name + ":" + Environment.NewLine + item.InnerText + Environment.NewLine + Environment.NewLine;
}

tbTest.Text = AnswersbyQuestion;


ExitEarly: ;
}

private void cbLoadTest_Click(object sender, EventArgs e)
{
OpenFileDialog openFileDialog2 = new OpenFileDialog();
openFileDialog2.Filter = "XML Files|*.xml";
openFileDialog2.Title = "Select a Test Answer Key";

if (openFileDialog2.ShowDialog() != DialogResult.OK)
{
MessageBox.Show("You have cancelled loading of the Test Answer Key. Please select another file to continue.");
goto ExitEarly;
}

xmlAnswerKey = new XmlDocument();
XPathNavigator zNav = xmlAnswerKey.CreateNavigator();
try
{
xmlAnswerKey.Load(openFileDialog2.FileName);
}
catch (XmlException error)
{
MessageBox.Show("Issue opening the file " + openFileDialog2.FileName + " . Error: " + error.ToString());
goto ExitEarly;
}

XmlNode root = xmlAnswerKey.DocumentElement;

XmlNodeList nodeList = null;

try
{
nodeList = root.SelectNodes("//weight");
}
catch (XPathException error)
{
MessageBox.Show("Sorry! There was an error locating the weighting in your answer key -- please ensure your key contains weights and try again. Error: " + error.ToString());
goto ExitEarly;
}

decimal WeightTotal = decimal.Zero;

foreach (XmlNode item in nodeList)
{
var num = decimal.Parse(item.InnerText.TrimEnd(new char[] { '%', ' ' })) / 100M;
WeightTotal = WeightTotal + num;
}

if (WeightTotal != 1)
{
MessageBox.Show("The weights in your test key do not add to 100%! Please edit and upload a new key accordingly.");
xmlAnswerKey = null;
}

zNav.MoveToRoot();
zNav.MoveToFirstChild();

tbAnswers.Text = zNav.OuterXml.ToString();

ExitEarly: ;
}

private void cbGrade_Click(object sender, EventArgs e)
{
  
XmlNode rootTest = xmlAnswers.DocumentElement;
XmlNode rootKey = xmlAnswerKey.DocumentElement;

XmlNodeList nodeListTest = rootTest.SelectNodes("//answer");
XmlNodeList nodeListAnswers = rootKey.SelectNodes("//answer");

if (nodeListTest.Count != nodeListAnswers.Count)
{
MessageBox.Show("The test and the key uploaded do not contain the same number of answers. Please review your key and load a new one to match the test loaded.");
goto ExitEarly;
}

List<string> gradedquestions = new List<string>();
char[] delimiter = {'|'};
string[] correctanswers = null;
decimal totalscore = 0;

for (int icounter = 0; icounter < nodeListAnswers.Count; icounter++)
{
int correctanswertotal = 0;
for (int children = 0; children < nodeListAnswers[icounter].ChildNodes.Count; children++)
{
correctanswers = nodeListAnswers[icounter].ChildNodes[children].InnerText.Split(delimiter);

bool correctresponse = false;
foreach (var answer in correctanswers)
{
if (nodeListTest[icounter].InnerText.ToUpper().IndexOf(answer.ToUpper(), 0) != -1)
{
correctresponse = true;
}
}

if (correctresponse == true)
{
correctanswertotal = correctanswertotal + 1;
}

}
double questionscore = Math.Round(((double) correctanswertotal / nodeListAnswers[icounter].ChildNodes.Count),2);

decimal questionworth = decimal.Parse(rootKey.SelectSingleNode("//" + nodeListAnswers[icounter].ParentNode.Name + "/weight").InnerText.TrimEnd(new char[] { '%', ' ' })) / 100M;

decimal questiongrade = Math.Round((System.Convert.ToDecimal(questionscore) * questionworth) * 100M,2);

string questiongradestring = questiongrade.ToString("00.00") + "%";

totalscore = totalscore + questiongrade;

gradedquestions.Add(nodeListTest[icounter].ParentNode.Name + " | " + questiongradestring + " out of " + (questionworth * 100M).ToString("00.00") + "%");
}

listBox1.DataSource = gradedquestions;

tbTotal.Text = totalscore.ToString() + "%";

ExitEarly:;
}

private void cbOutput_Click(object sender, EventArgs e)
{
XmlNode rootTest = xmlAnswers.DocumentElement;
XmlNode rootKey = xmlAnswerKey.DocumentElement;

XmlNodeList nodeListTest = rootTest.SelectNodes("//answer");

foreach (XmlNode item in nodeListTest)
{
item.AppendChild()
}

}
}
}

Explanation / Answer

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Xml;
using System.Xml.XPath;
namespace Assignment2
{
public partial class Form1 : Form
{
private XmlDocument xmlAnswerKey;
private XmlDocument xmlAnswers;
public Form1()
{
InitializeComponent();
}
private void cbLoadAnswer_Click(object sender, EventArgs e)
{
OpenFileDialog openFileDialog1 = new OpenFileDialog();
openFileDialog1.Filter = "XML Files|*.xml";
openFileDialog1.Title = "Select a Test to Grade";
if (openFileDialog1.ShowDialog() != DialogResult.OK)
{
MessageBox.Show("You have cancelled loading of the Test. Please select another file to continue.");
goto ExitEarly;
}
xmlAnswers = new XmlDocument();
XPathNavigator xNav = xmlAnswers.CreateNavigator();
try
{
xmlAnswers.Load(openFileDialog1.FileName);
}
catch (XmlException error)
{
MessageBox.Show("Issue opening the file " + openFileDialog1.FileName + " . Error: " + error.ToString());
goto ExitEarly;
}

XmlNode root = xmlAnswers.DocumentElement;
XmlNodeList nodeList = null;
try
{
nodeList = root.SelectNodes("//answer");
}
catch (XPathException error)
{
MessageBox.Show("Sorry! There was an error locating the answers in this exam. Error: " + error.ToString());
goto ExitEarly;
}
var sw = new System.IO.StringWriter();
var xw = new System.Xml.XmlTextWriter(sw);
string AnswersbyQuestion = string.Empty;
foreach (XmlNode item in nodeList)
{
AnswersbyQuestion = AnswersbyQuestion + item.ParentNode.Name + ":" + Environment.NewLine + item.InnerText + Environment.NewLine + Environment.NewLine;
}
tbTest.Text = AnswersbyQuestion;

ExitEarly: ;
}
private void cbLoadTest_Click(object sender, EventArgs e)
{
OpenFileDialog openFileDialog2 = new OpenFileDialog();
openFileDialog2.Filter = "XML Files|*.xml";
openFileDialog2.Title = "Select a Test Answer Key";
if (openFileDialog2.ShowDialog() != DialogResult.OK)
{
MessageBox.Show("You have cancelled loading of the Test Answer Key. Please select another file to continue.");
goto ExitEarly;
}
xmlAnswerKey = new XmlDocument();
XPathNavigator zNav = xmlAnswerKey.CreateNavigator();
try
{
xmlAnswerKey.Load(openFileDialog2.FileName);
}
catch (XmlException error)
{
MessageBox.Show("Issue opening the file " + openFileDialog2.FileName + " . Error: " + error.ToString());
goto ExitEarly;
}
XmlNode root = xmlAnswerKey.DocumentElement;
XmlNodeList nodeList = null;
try
{
nodeList = root.SelectNodes("//weight");
}
catch (XPathException error)
{
MessageBox.Show("Sorry! There was an error locating the weighting in your answer key -- please ensure your key contains weights and try again. Error: " + error.ToString());
goto ExitEarly;
}
decimal WeightTotal = decimal.Zero;
foreach (XmlNode item in nodeList)
{
var num = decimal.Parse(item.InnerText.TrimEnd(new char[] { '%', ' ' })) / 100M;
WeightTotal = WeightTotal + num;
}
if (WeightTotal != 1)
{
MessageBox.Show("The weights in your test key do not add to 100%! Please edit and upload a new key accordingly.");
xmlAnswerKey = null;
}
zNav.MoveToRoot();
zNav.MoveToFirstChild();
tbAnswers.Text = zNav.OuterXml.ToString();
ExitEarly: ;
}
private void cbGrade_Click(object sender, EventArgs e)
{
  
XmlNode rootTest = xmlAnswers.DocumentElement;
XmlNode rootKey = xmlAnswerKey.DocumentElement;
XmlNodeList nodeListTest = rootTest.SelectNodes("//answer");
XmlNodeList nodeListAnswers = rootKey.SelectNodes("//answer");
if (nodeListTest.Count != nodeListAnswers.Count)
{
MessageBox.Show("The test and the key uploaded do not contain the same number of answers. Please review your key and load a new one to match the test loaded.");
goto ExitEarly;
}
List<string> gradedquestions = new List<string>();
char[] delimiter = {'|'};
string[] correctanswers = null;
decimal totalscore = 0;
for (int icounter = 0; icounter < nodeListAnswers.Count; icounter++)
{
int correctanswertotal = 0;
for (int children = 0; children < nodeListAnswers[icounter].ChildNodes.Count; children++)
{
correctanswers = nodeListAnswers[icounter].ChildNodes[children].InnerText.Split(delimiter);
bool correctresponse = false;
foreach (var answer in correctanswers)
{
if (nodeListTest[icounter].InnerText.ToUpper().IndexOf(answer.ToUpper(), 0) != -1)
{
correctresponse = true;
}
}
if (correctresponse == true)
{
correctanswertotal = correctanswertotal + 1;
}
}
double questionscore = Math.Round(((double) correctanswertotal / nodeListAnswers[icounter].ChildNodes.Count),2);
decimal questionworth = decimal.Parse(rootKey.SelectSingleNode("//" + nodeListAnswers[icounter].ParentNode.Name + "/weight").InnerText.TrimEnd(new char[] { '%', ' ' })) / 100M;
decimal questiongrade = Math.Round((System.Convert.ToDecimal(questionscore) * questionworth) * 100M,2);
string questiongradestring = questiongrade.ToString("00.00") + "%";
totalscore = totalscore + questiongrade;
gradedquestions.Add(nodeListTest[icounter].ParentNode.Name + " | " + questiongradestring + " out of " + (questionworth * 100M).ToString("00.00") + "%");
}
listBox1.DataSource = gradedquestions;
tbTotal.Text = totalscore.ToString() + "%";
ExitEarly:;
}
private void cbOutput_Click(object sender, EventArgs e)
{
XmlNode rootTest = xmlAnswers.DocumentElement;
XmlNode rootKey = xmlAnswerKey.DocumentElement;
XmlNodeList nodeListTest = rootTest.SelectNodes("//answer");
foreach (XmlNode item in nodeListTest)
{
item.AppendChild()
}
}
}
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote