0

I have this classes:

[Serializable]
    [XmlRoot(ElementName = "allTheQuestions")]
    public class AllQuestions
    {
        [XmlArray("quests")]
        public List<Question> questions { get; set; }
    }

    [Serializable]
    [XmlType(TypeName="quest")]
    public class Question
    {
        public string enunciado { get; set; }
        [XmlIgnore]
        public int CorrectOptionIndex { get; set; }
        [XmlArray("itens")]
        public List<Item> options { get; set; }
    }


    [Serializable]
    public class Item
    {
        [XmlIgnore]
        public int correctIndex { get; set; }
        [XmlIgnore]
        public int index { get; set; }

        [XmlAttribute("correct")]
        public string IsCorrect
        {
            get { return correctIndex == index ? "1" : "0"; }
            set { } // XmlSerializer won't work without a set with a body defined
        }

        [XmlAttribute("order")]
        public string Order
        {
            get 
            {
                char ch = (char) ('a' + index);

                if (ch > 'z')
                    throw new InvalidOperationException("'ch' canno't be greater than 'z'");

                return string.Format("{0})", ch);
            }
            set { } // XmlSerializer won't work without a set with a body defined
        }

        [XmlText]
        public string text { get; set; }
    }

Serializing using this:

var list = new List<Item>();
list.Add(new Item() { correctIndex = 10, index = 11, text = "asdasd" });
list.Add(new Item() { correctIndex = 10, index = 12, text = "asdasaad" });  
var questions = new List<Question>();
questions.Add(new Question { enunciado = "alalal", CorrectOptionIndex = 3, options = list });
var all = new AllQuestions { questions = questions };

var filename = @"C:\Users\lucas\Desktop\foo.xml";
using (var sw = new StreamWriter(filename))
        {
            var emptyNamepsaces = new XmlSerializerNamespaces(new[] { XmlQualifiedName.Empty });
            var serializer = new XmlSerializer(typeof(AllQuestions));
            serializer.Serialize(sw, all, emptyNamepsaces);
        }

Generate this:

<?xml version="1.0" encoding="utf-8"?>

<allTheQuestions>

  <quests>

    <quest>

      <enunciado>alalal</enunciado>

      <itens>

        <Item correct="0" order="l)">asdasd</Item>

        <Item correct="0" order="m)">asdasaad</Item>

      </itens>

    </quest>

  </quests>

</allTheQuestions>

How do I remove the <allTheQuestions>?

It must be:

  <quests>

    <quest>

      <enunciado>alalal</enunciado>

      <itens>

        <Item correct="0" order="l)">asdasd</Item>

        <Item correct="0" order="m)">asdasaad</Item>

      </itens>

    </quest>

  </quests>
Jack
  • 16,276
  • 55
  • 159
  • 284

1 Answers1

1

Instead of serializing AllQuestions, serialize simply questions

Your serialization code would be similar to this...

var list = new List<Item>();
list.Add(new Item() { correctIndex = 10, index = 11, text = "asdasd" });
list.Add(new Item() { correctIndex = 10, index = 12, text = "asdasaad" });
var questions = new List<Question>();
questions.Add(new Question { enunciado = "alalal", CorrectOptionIndex = 3, options = list });

var m = new MemoryStream();
using (var sw = new StreamWriter(m))
{
    var emptyNamepsaces = new XmlSerializerNamespaces(new[] { XmlQualifiedName.Empty });
    var serializer = new XmlSerializer(questions.GetType(), new XmlRootAttribute("quests"));
    serializer.Serialize(sw, questions, emptyNamepsaces );

    var xml = Encoding.UTF8.GetString(m.ToArray());
}

The only trick is

var serializer = new XmlSerializer(questions.GetType(), new XmlRootAttribute("quests"));
EZI
  • 15,209
  • 2
  • 27
  • 33
  • Thanks,I was missing this contructor overload. Can I force the XML serialization to save the enum integer value rather than the name? when I tried to serialize a property of type `enum X { none = 0, a = 2 }` with value of `X.A` it saves the `A` (enum's name) rather than the value `2`? – Jack Aug 04 '15 at 21:18
  • Jack, this is a `Q&A` site, not `Q&A,Q&A,Q&A,...` – EZI Aug 04 '15 at 21:21
  • you're right, sorry. I'll open another theread. Thanks for your help – Jack Aug 04 '15 at 21:25
  • 1
    If you're going to use the [`XmlSerializer (Type, XmlRootAttribute)`](https://msdn.microsoft.com/en-us/library/f1wczcys%28v=vs.110%29.aspx) constructor, you must cache it along the lines of [XmlSerializer Performance Issue when Specifying XmlRootAttribute](http://stackoverflow.com/questions/1534810/xmlserializer-performance-issue-when-specifying-xmlrootattribute). – dbc Aug 04 '15 at 23:07