C# 如何在XML序列化中插入XML注释?

C# 如何在XML序列化中插入XML注释?,c#,xml,xml-serialization,C#,Xml,Xml Serialization,我想在xml文件的顶部添加一些注释,供阅读该文件的用户使用。但我不知道如何使用xml序列化来实现这一点 我在看这篇文章 但我不确定到底发生了什么,以及如何将其添加到我的代码中。基本上,我只是将一些类序列化为xml并将其粘贴到内存流中 所以我不确定我应该在什么时候添加这些评论 谢谢 代码 using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Xml.Se

我想在xml文件的顶部添加一些注释,供阅读该文件的用户使用。但我不知道如何使用xml序列化来实现这一点

我在看这篇文章

但我不确定到底发生了什么,以及如何将其添加到我的代码中。基本上,我只是将一些类序列化为xml并将其粘贴到内存流中

所以我不确定我应该在什么时候添加这些评论

谢谢

代码

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Serialization;

namespace ConsoleApplication1
{
    [XmlRoot("Course")]
    public class MyWrapper 
    {
        public MyWrapper()
        {
            TaskList = new List<Tasks>();
        }

        [XmlElement("courseName")]
        public string CourseName { get; set; }

        [XmlElement("backgroundColor")]
        public string BackgroundColor { get; set; }

        [XmlElement("fontColor")]
        public string  FontColor { get; set; }

        [XmlElement("sharingKey")]
        public Guid SharingKey { get; set; }

        [XmlElement("task")]
        public List<Tasks> TaskList { get; set; }

    }

public class Tasks
{
    [XmlAttribute("type")]
    public string Type { get; set; }

    [XmlElement("taskName")]
    public string TaskName { get; set; }

    [XmlElement("description")]
    public string Description { get; set; }

    [XmlElement("taskDueDate")]
    public DateTime TaskDueDate { get; set; }

    [XmlElement("weight")]
    public decimal? Weight { get; set; }

    [XmlElement("beforeDueDateNotification")]
    public int BeforeDueDateNotification { get; set; }

    [XmlElement("outOf")]
    public decimal? OutOf { get; set; }

}

只需将XmlWriter作为MemoryStream和XmlSerializer之间的中间级别:

static public MemoryStream SerializeToXML(MyWrapper list)
{
    XmlSerializer serializer = new XmlSerializer(typeof(MyWrapper));
    MemoryStream stream = new MemoryStream();
    XmlWriter writer = XmlWriter.Create(stream);
    writer.WriteStartDocument();
    writer.WriteComment("Product XY Version 1.0.0.0");
    serializer.Serialize(writer, course);
    writer.WriteEndDocument();
    writer.Flush();
    return stream;
}

您可以在序列化对象图之前和之后添加任何XML(只要结果是有效的XML)。

(我为问题中链接的答案添加了替代解决方案。)好的,我添加了代码。因此,您可以看到我正在做什么,以及我应该在哪里添加代码。默认情况下,文档不会缩进/格式化。因此需要在构造函数中设置它:XmlWriter.Create(stream,newxmlwritersettings{Indent=true});或者使用XmlTextWriter:XmlTextWriter=XmlTextWriter.Create(流);writer.Formatting=格式化.缩进;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Serialization;
using System.IO;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {

            MyWrapper wrap = new MyWrapper();
            wrap.CourseName = "Comp 1510";
            wrap.FontColor = "#ffffff";
            wrap.BackgroundColor = "#ffffff";
            wrap.SharingKey = Guid.NewGuid();

            Tasks task = new Tasks()
            {
                TaskName = "First Task",
                Type = "Assignment",
                TaskDueDate = DateTime.Now,
                Description = "description",
                BeforeDueDateNotification = 30,
                OutOf = 50.4M
            };

            wrap.TaskList.Add(task);
           var stream = SerializeToXML(wrap);


        }

        static public MemoryStream SerializeToXML(MyWrapper list)
        {

            XmlSerializer serializer = new XmlSerializer(typeof(MyWrapper));
            MemoryStream stream = new MemoryStream();
            serializer.Serialize(stream, course);
            return stream;  


        }

    }
}
static public MemoryStream SerializeToXML(MyWrapper list)
{
    XmlSerializer serializer = new XmlSerializer(typeof(MyWrapper));
    MemoryStream stream = new MemoryStream();
    XmlWriter writer = XmlWriter.Create(stream);
    writer.WriteStartDocument();
    writer.WriteComment("Product XY Version 1.0.0.0");
    serializer.Serialize(writer, course);
    writer.WriteEndDocument();
    writer.Flush();
    return stream;
}