I am trying to learn serialization whilst making a quiz game in C#.
I have a Questions class in which I have a method WriteToXml.
Trouble is the object data is not being written to my xml file I do not understand why?
I want to pass in a Question object that is written to my XML file instead of creating the object inside the WriteToXML method.
Here is my code:
namespace QuizGame
{
class Program
{
static void Main(string[] args)
{
Question q1 = new Question();
q1.CreateAQuestion("How many players in a football team?", "12", "10", "15", "11");
q1.WriteToXmlFile(q1);
Console.ReadLine();
}
}
public class Question
{
#region Constructor
public Question()
{
}
#endregion
#region Public Procedures
/// <summary>
/// Create a question for your quiz
/// </summary>
public void CreateAQuestion(string theQ, string opt1, string opt2, string opt3, string theAnswer)
{
theQuestion = theQ;
answerA = opt1;
answerB = opt2;
answerC = opt3;
correctAnswer = theAnswer;
}
/// <summary>
/// write quiz questions to xmlFile
/// </summary>
/// <param name="q"></param>
public void WriteToXmlFile(Question q)
{
//write data to the xml file
XmlSerializer textWriter = new XmlSerializer(typeof(Question));
StreamWriter xmlFile = new StreamWriter(@"C:\Development\Learning\Files\qsFile.xml");
textWriter.Serialize(xmlFile, q);
xmlFile.Close();
}
#endregion
#region Private Properties
private string theQuestion { get; set; }
private string answerA { get; set; }
private string answerB { get; set; }
private string answerC { get; set; }
private string correctAnswer { get; set; }
#endregion
}
}
What am I missing ?
Thank you