简体   繁体   English

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

[英]C# XML Insert comment into XML after xml tag

I am using a C# object to Serialize/Deserialize XML. 我正在使用C#对象来序列化/反序列化XML。

I would like to add a comment to the XML file whilst serializing, this comment will be a basic <!-- comment --> after the standard xml tag <?xml version="1.0" encoding="UTF-8"?> 我希望在序列化时为XML文件添加注释,这个注释将是标准xml标签<?xml version="1.0" encoding="UTF-8"?>之后的基本<!-- comment --> <?xml version="1.0" encoding="UTF-8"?>

This comment does not need to be deserialized, its a basic comment to indicate the product and version that created the xml file. 此注释不需要反序列化,它是一个基本注释,用于指示创建xml文件的产品和版本。

You can serialize directly into a new XDocument using CreateWriter : 您可以使用CreateWriter直接序列化到新的XDocument

XDocument document = new XDocument();
document.Add(new XComment("Product XY Version 1.0.0.0"));
using (XmlWriter writer = document.CreateWriter())
{
    serializer.WriteObject(writer, graph);
}
document.Save(Console.Out);

Alternatively, you can serialize into any other XmlWriter as well: 或者,您也可以序列化到任何其他XmlWriter

using (XmlWriter writer = XmlWriter.Create(Console.Out))
{
    writer.WriteStartDocument();
    writer.WriteComment("Product XY Version 1.0.0.0");
    serializer.WriteObject(writer, graph);
    writer.WriteEndDocument();
}

Serialize it to XML, load that XML as an XDocument (or whatever API you want), insert the comment, save it out again. 将其序列化为XML,将该XML作为XDocument (或任何您想要的API)加载,插入注释,再次保存。 Simple, and should work with whatever API you want to use. 简单,并且应该使用您想要使用的任何API。 You can do it all in memory using a MemoryStream as the temporary storage. 您可以使用MemoryStream作为临时存储在内存中完成所有操作。

There may be a way of serializing directly into a new XDocument / XmlDocument , but I'm not aware of it. 可能有一种方法可以直接序列化到新的XDocument / XmlDocument ,但我不知道它。

I believe you can implement IXMLSeriablizable on your objects. 我相信你可以在你的对象上实现IXMLSeriablizable。 If I remember correctly, ReadXML(XmlReader reader) and WriteXML(XmlWriter writer) from that interface are called automatically when serializing/de-serializing (CreateSchema, or whatever the third method is, doesn't need to be implemented). 如果我没记错的话,序列化/反序列化时会自动调用该接口的ReadXML(XmlReader reader)和WriteXML(XmlWriter writer)(CreateSchema,或者不需要实现第三种方法)。

The caveat of implementing it is that you now may need to implement it on all related nested objects. 实现它的警告是,您现在可能需要在所有相关的嵌套对象上实现它。 (ie if you have a Department object that contains User objects and you want the comment on Departments, you will need to implement IXmlSeriablizable on both Department and User). (即,如果您有一个包含User对象的Department对象,并且您希望对Departments进行注释,则需要在Department和User上实现IXmlSeriablizable)。 Also, since you are managing the serialization directly, if you add a new property to a class, you will manually need to modify the IXmlSerializable methods. 此外,由于您直接管理序列化,如果向类添加新属性,则需要手动修改IXmlSerializable方法。

I typically implement IXmlSerializable on my objects, as I like having direct control over what gets serialized and how. 我通常在我的对象上实现IXmlSerializable,因为我喜欢直接控制序列化的内容和方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM