c# - 需要在现有的 xml 文档中添加注释

标签 c#

我需要在现有的 xml 文档中添加注释。示例 xml 如下所示我需要用 c# 编写代码。 XML 序列化用于生成此 xml 任何帮助都会很棒... 提前致谢

<?xml version="1.0" encoding="utf-8"?>
<Person>
<Name>Job</Name>
<Address>10dcalp</Address>
<Age>12</Age>
</Person>

最佳答案

像这样尝试:

        string input = @"<?xml version=""1.0"" encoding=""utf-8""?><Person><Name>Job</Name><Address>10dcalp</Address><Age>12</Age></Person>";
        XDocument doc = XDocument.Parse(input);
        XElement age = doc.Root.Element("Age");
        XComment comm = new XComment("This is comment before Age");
        age.AddBeforeSelf(comm);

此代码获取文档,找到预期位于根元素(“Person”)下的名为“Age”的元素,并在其之前添加注释。

关于c# - 需要在现有的 xml 文档中添加注释,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11465194/

相关文章:

c# - 在紧凑型和完整 .net 框架之间切换编译?

C# : Console. Read() 未获取 "right"输入

c# - 选择nhibernate二级缓存方案时考虑的因素有哪些

c# - 在 c# 中将整数列表附加到 SQL Server 查询。我怎样才能做到这一点?

c# - Guid 字符串应仅包含十六进制字符

c# - 无法在 SignalR 中将类型 'System.Threading.Tasks.Task<object>' 隐式转换为 'string'

c# - 使用 Linq 从列表中获取所有匹配值的索引

c# - 使用 DataContractSerializer 序列化时删除已知类型的命名空间

c# - 从最小/最大场景计算步骤,步骤应该是很好的数字 c#

c# - 这是 base40 吗?如果是,我如何在 C# 中对其进行解码?