在序列化时省略 root class

Omit root class on serialization

我有这个类:

[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; }
    }

使用这个进行序列化:

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);
        }

生成这个:

<?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>

如何删除 <allTheQuestions>

必须是:

  <quests>

    <quest>

      <enunciado>alalal</enunciado>

      <itens>

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

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

      </itens>

    </quest>

  </quests>

不是序列化 AllQuestions,而是简单地序列化 questions

您的序列化代码与此类似...

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());
}

唯一的诀窍是

var serializer = new XmlSerializer(questions.GetType(), new XmlRootAttribute("quests"));