C#XML在xml标记后将注释插入XML [英] C# XML Insert comment into XML after xml tag

查看:83
本文介绍了C#XML在xml标记后将注释插入XML的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用C#对象对XML进行序列化/反序列化.

I am using a C# object to Serialize/Deserialize XML.

我想在序列化时向XML文件添加注释,此注释将是标准xml标记<之后的基本<!-comment-> .?xml version ="1.0" encoding ="UTF-8"?>

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文件的产品和版本.

This comment does not need to be deserialized, its a basic comment to indicate the product and version that created the xml file.

推荐答案

将其序列化为XML,将该XML作为 XDocument (或所需的任何API)加载,插入注释,然后将其保存再次.很简单,并且可以与您要使用的任何API一起使用.您可以使用 MemoryStream 作为临时存储在内存中完成所有操作.

Serialize it to XML, load that XML as an XDocument (or whatever API you want), insert the comment, save it out again. Simple, and should work with whatever API you want to use. You can do it all in memory using a MemoryStream as the temporary storage.

可能有 一种直接序列化为新的 XDocument / XmlDocument 的方法,但我不知道.

There may be a way of serializing directly into a new XDocument/XmlDocument, but I'm not aware of it.

这篇关于C#XML在xml标记后将注释插入XML的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆