c# - 通过代码C#在xml中的特定位置添加注释

标签 c# xml

我知道我可以使用 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/

相关文章:

c# - 如何将数据从 Windows 窗体保存到 XML 文件?

xml - haskell xml使用HXT库更新文本

c# - <ImportWindowsDesktopTargets> 在 try-convert 生成的 *.csproj 中做什么?

c# - 如何才能使只有登录用户才能查看自己的成绩?

c# - 使用 LINQ 计算两个字符串之间的汉明距离

c# - 在不剥离标签的情况下获取 HTML 内容的前 100 个字符

c - C 的最佳 XML 解析器

c# - 请求的资源正在使用中。 (来自 HRESULT : 0x800700AA) - Microsoft WebView 的异常

xml - XML 命名空间名称 URI 必须是可检索的吗?

android - 应用程序 :srcCompat in xml 的 InflateException