我知道我可以使用 XElement 生成 XML,
比如
XElement xmlTree1 = new XElement("Root",
new XElement("Child3", 3),
new XElement("Child4", 4),
new XElement("Child5", 5),
);
Console.WriteLine(xmlTree2);
结果是
<Root>
<Child3>3</Child3>
<Child4>4</Child4>
<Child5>5</Child5>
</Root>
.
我的问题是,有没有什么方法可以通过代码使用 C# 在 xml 中的特定位置添加注释?
.
我想做的是这样的
XElement xmlTree1 = new XElement("Root",
new XElement("Child3", 3),
new XElement("Child4", 4),
//Maybe Some Way to Add Comment Like This
new COMMENT(String.Format("This is a comment test!!!!")),
new XElement("Child5", 5),
);
Console.WriteLine(xmlTree2);
结果是
<Root>
<Child3>3</Child3>
<Child4>4</Child4>
<!-- This is a comment test!!!! -->
<Child5>5</Child5>
</Root>
谢谢
最佳答案
我认为答案就像使用 XComment
一样简单:
XElement xmlTree1 = new XElement("Root",
new XElement("Child3", 3),
new XElement("Child4", 4),
//Maybe Some Way to Add Comment Like This
new XComment(String.Format("This is a comment test!!!!")),
new XElement("Child5", 5),
);
关于c# - 通过代码C#在xml中的特定位置添加注释,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37473848/