1

I want to create an XML file like the above through C# serialization, what could be the class structure for this file ? So that it would generate desired XML file after serialization.

<Subjects>
    <Subject>Maths</Subject>
    <Questions>
        <Question>Maths question 1</Question>
        <Answer>Maths answer 1</Answer>
        <Marks>10</Marks>
    </Questions>
    <Questions>
        <Question>Maths question 2</Question>
        <Answer>Maths answer 2</Answer>
        <Marks>5</Marks>
    </Questions>
    <Questions>
        <Question>Maths question 3</Question>
        <Answer>Maths answer 3</Answer>
        <Marks>20</Marks>
    </Questions>
</Subjects>
<Subjects>
    <Subject>Science</Subject>
    <Questions>
        <Question>Science question 1</Question>
        <Answer>Science answer 3</Answer>
        <Marks>20</Marks>
    </Questions>
    <Questions>
        <Question>Science question 2</Question>
        <Answer>Science answer 3</Answer>
        <Marks>10</Marks>
    </Questions>
    <Questions>
        <Question>Science question 3</Question>
        <Answer>Science answer 3</Answer>
        <Marks>20</Marks>
    </Questions>
</Subjects>

I am trying with this class structure -

 public class Subjects
    {
        public string Subject
        {
            get; set;
        }
        public List<Questions> Questions
        {
            get; set;
        }
    }

    public class Questions
    {
        public string Question
        {
            get; set;
        }
        public string Answer
        {
            get; set;
        }
        public string Marks
        {
            get; set;
        }
    }

but it is not generating desired XML file after serialization using XMLSerializer.

akkipedia
  • 11
  • 2
  • 4
    If you have Visual Studio: Copy the xml text, go to visual studio and open the edit menu, select paste special and choose paste as xml classes. – Silvermind Nov 30 '17 at 08:00
  • 1
    Before asking, did you consider any specific answers yourself? What are the difficult aspects, do you think? It doesn't help that you haven't provided a complete XML file - what you've shown would have two root elements... – Jon Skeet Nov 30 '17 at 08:05
  • Please don't add it in comments - edit your question. Have you read the question this one is now marked as a duplicate of? – Jon Skeet Nov 30 '17 at 13:18

0 Answers0