0

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

xirokx
  • 37
  • 7

1 Answers1

1

Properties must be public :

[Serializable]  // Not needed
public class Question
{
  #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
    var textWriter = new XmlSerializer(typeof (Question));
    var xmlFile = new StreamWriter(@"d:\qsFile.xml");
    textWriter.Serialize(xmlFile, q);
    xmlFile.Close();
  }

  #endregion

  #region Private Properties

  public string theQuestion { get; set; }
  public string answerA { get; set; }
  public string answerB { get; set; }
  public string answerC { get; set; }
  public string correctAnswer { get; set; }

  #endregion
}
  • 2
    The `[Serializable]` attribute has nothing to do with `XmlSerializer`. – Charles Mager Jun 30 '16 at 14:44
  • 1
    Take a look at original code: "private string theQuestion" all properties are private –  Jun 30 '16 at 14:46
  • I made the properties public and it worked. Thank you. Stupid q I know but why can you not serialize private properties ...Also it works without adding [serializable] – xirokx Jun 30 '16 at 14:50
  • [Serializeable] is just a habit from me for more than 10 years...:=) –  Jun 30 '16 at 14:52
  • "XML serialization is the process of converting an object's public properties and fields to a serial format (in this case, XML)" https://msdn.microsoft.com/en-us/library/system.xml.serialization.xmlserializer(v=vs.110).aspx – Paul Zahra Jun 30 '16 at 15:03