gpt4 book ai didi

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

转载 作者:数据小太阳 更新时间:2023-10-29 01:44:17 25 4
gpt4 key购买 nike

我想在我的 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com