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

cho*_*bo2 8 c# xml xml-serialization

我想在我的xml文件的顶部添加一些用于读取它的用户的注释.我不知道如何使用xml序列化来做到这一点.

我在看这篇文章

C#XML在xml标记之后将注释插入XML

XDocument document = new XDocument();
document.Add(new XComment("Product XY Version 1.0.0.0"));
using (var writer = document.CreateWriter())
{
    serializer.WriteObject(writer, graph);
}
document.Save(Console.Out);
Run Code Online (Sandbox Code Playgroud)

但我不确定发生了什么以及如何将其添加到我的代码中.基本上我只有一些类,我将其序列化为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; }

}
Run Code Online (Sandbox Code Playgroud)

}

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;  


        }

    }
}
Run Code Online (Sandbox Code Playgroud)

dtb*_*dtb 17

只需将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;
}
Run Code Online (Sandbox Code Playgroud)

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

  • 默认情况下,文档不会缩进/格式化.所以你需要在构造函数中设置它:XmlWriter.Create(stream,new XmlWriterSettings {Indent = true}); 或者使用XmlTextWriter:XmlTextWriter writer = XmlTextWriter.Create(stream); writer.Formatting = Formatting.Indented; (5认同)