作者热门文章
- xml - AJAX/Jquery XML 解析
- 具有多重继承的 XML 模式
- .net - 枚举序列化 Json 与 XML
- XML 简单类型、简单内容、复杂类型、复杂内容
我想在我的 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/
我是一名优秀的程序员,十分优秀!