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

标签 c# xml xml-serialization

我想在我的 xml 文件的顶部为阅读它的用户添加一些注释。我不确定如何使用 xml 序列化来执行此操作。

我在看这篇文章

C# XML Insert comment into XML after xml tag

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

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

}

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;  


        }

    }
}

最佳答案

只需将 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)。

关于c# - 如何在 XML 序列化中插入 XML 注释?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2129414/

相关文章:

java - 使用 apache.commons.configuration.tree.ConfigurationNode.getValue() 在标签值中包含 ","时未获取整个标签值

c# - 如何在 C# 中使用 Firebird 创建嵌入式数据库

c# - 如何根据姓氏和名字在 Active Directory 中搜索用户?

xml - 仅限制 XSD 中可变大小元素中的数字字符

c# - Xelement - 获取元素

c# - 如何在 .Net 中序列化 COM 对象?

c# - 在 C# 中使用 WPF 正确绘制图表

c# - CreateLogger() 之前被调用过,只能调用一次

java - 使用序列化是个好主意吗?

c# - 如何告诉 XmlSerializer 始终使用 [DefautValue(...)] 序列化属性?