Good day! How to save data about objects that do not know about other objects into one XML file?
I have the following classes: Test - which does not know about anything other than its data. The question is - who knows about the test for which he was created and about his data. The answer is - who knows about the question to which it was created and about its data.
Test class
[XmlRoot(ElementName = "Test")] public class Test { public int Id { get; set; } public string Name { get; set; } public string Theory { get; set; } public bool IsShowTheory { get; set; } public string URLtoTheory { get; set; } public DateTime Date { get ; set; } public int CountRandomQoestions { get; set; } public int CountCorrectAnswers { get; set; } private Test() { } public Test(int id, string name, string theory, bool isShowTheory, string urlToTheory, DateTime date, int countRandomQuestion, int countCorrectAnswers) { Id = id; Name = name; Theory = theory; IsShowTheory = isShowTheory; URLtoTheory = urlToTheory; Date = date; CountCorrectAnswers = countCorrectAnswers; CountRandomQoestions = countRandomQuestion; } } Class question
[XmlRoot(ElementName = "QUestion")] public class Question { public int Id { get; } [XmlElement("Test", typeof(Test))] public Test Test { get; } public string Text { get; } private Question() { } public Question(int id, Test test, string text) { Id = id; Test = test; Text = text; } } Class Answer
[XmlRoot(ElementName = "Answer")] public class Answer { public int Id { get; set; } [XmlElement("Question", typeof(Question))] public Question Question { get; set; } public string Text { get; set; } public bool IsTrue { get; set; } private Answer() { } public Answer(int id, Question question, string text, bool isTrue) { Id = id; Question = question; Text = text; IsTrue = isTrue; } } I create a test like this:
Test result = new Test(0, "C# basic", "Look in the WIKI", true, @"http:\\Wikipedia.ru", DateTime.Now, 2, 1); Question objectQuestion = new Question(0, result ,"What is the general type?"); Answer objectAnswer1 = new Answer(0, objectQuestion, "string", false); Answer objectAnswer2 = new Answer(1, objectQuestion, "object", true); Answer objectAnswer3 = new Answer(2, objectQuestion, "int", false); Answer objectAnswer4 = new Answer(3, objectQuestion, "bool", false); How do these objects serialize to a similar xml file?
<Test> <Name> Основы C#. </Name> <Theory> Язык C# (c-sharp, си шарп) - современный объектно-ориентированный язык программирования. Он был разработан компанией Microsoft как один из языков для платформы .NET (.NET framework). Язык c# в наше время очень широко развит и продолжает развиваться: в нем появляются новые возможности, языковые конструкции, пополняется библиотека классов. </Theory> <IShowTheory> true </IShowTheory> <URLtoTheory> [url]https://ru.wikipedia.org/wiki/C_Sharp[/url] </URLtoTheory> <CountRandomQuestion> 4 </CountRandomQuestion> <CountCorrectAnswers> 3 </CountCorrectAnswers> <Question> <Text> Какой класс является базовым для всех классов в С#? </Text> <AnswerVariant> <Text> String </Text> <IsTrue> false </IsTrue> </AnswerVariant> <AnswerVariant> <Text> object </Text> <IsTrue> true </IsTrue> </AnswerVariant> <AnswerVariant> <Text> int </Text> <IsTrue> false </IsTrue> </AnswerVariant> <AnswerVariant> <Text> bool </Text> <IsTrue> false </IsTrue> </AnswerVariant> </Question> </Test>