我需要在现有的 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/