ConfigurationManager 可以保留 Save() 上的 XML 注释吗?
我编写了一个小实用程序,允许我更改另一个应用程序的 App.config 文件的简单 AppSetting,然后保存更改:
//save a backup copy first.
var cfg = ConfigurationManager.OpenExeConfiguration(pathToExeFile);
cfg.SaveAs(cfg.FilePath + "." + DateTime.Now.ToFileTime() + ".bak");
//reopen the original config again and update it.
cfg = ConfigurationManager.OpenExeConfiguration(pathToExeFile);
var setting = cfg.AppSettings.Settings[keyName];
setting.Value = newValue;
//save the changed configuration.
cfg.Save(ConfigurationSaveMode.Full);
除了一个副作用之外,这效果很好。 新保存的.config文件会丢失所有原始XML注释,但仅限于AppSettings区域内。是否可以保留原始配置文件AppSettings区域中的XML注释?
I've written a small utility that allows me to change a simple AppSetting for another application's App.config file, and then save the changes:
//save a backup copy first.
var cfg = ConfigurationManager.OpenExeConfiguration(pathToExeFile);
cfg.SaveAs(cfg.FilePath + "." + DateTime.Now.ToFileTime() + ".bak");
//reopen the original config again and update it.
cfg = ConfigurationManager.OpenExeConfiguration(pathToExeFile);
var setting = cfg.AppSettings.Settings[keyName];
setting.Value = newValue;
//save the changed configuration.
cfg.Save(ConfigurationSaveMode.Full);
This works well, except for one side effect. The newly saved .config file loses all the original XML comments, but only within the AppSettings area. Is it possible to to retain XML comments from the original configuration file AppSettings area?
Here's a pastebin of the full source if you'd like to quickly compile and run it.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我跳进 Reflector.Net 并查看了该类的反编译源代码。简短的回答是否定的,它不会保留评论。 Microsoft 编写该类的方式是根据配置类的属性生成 XML 文档。由于注释不会出现在配置类中,因此它们不会返回到 XML 中。
更糟糕的是,微软密封了所有这些类,因此您无法派生新类并插入自己的实现。您唯一的选择是将注释移到 AppSettings 部分之外,或者使用
XmlDocument
或XDocument
类来解析配置文件。对不起。这是微软没有计划到的边缘情况。
I jumped into Reflector.Net and looked at the decompiled source for this class. The short answer is no, it will not retain the comments. The way Microsoft wrote the class is to generate an XML document from the properties on the configuration class. Since the comments don't show up in the configuration class, they don't make it back into the XML.
And what makes this worse is that Microsoft sealed all of these classes so you can't derive a new class and insert your own implementation. Your only option is to move the comments outside of the AppSettings section or use
XmlDocument
orXDocument
classes to parse the config files instead.Sorry. This is an edge case that Microsoft just didn't plan for.
这是一个可用于保存评论的示例函数。它允许您一次编辑一个键/值对。我还添加了一些内容,以便根据我常用的文件方式很好地格式化文件(如果需要,您可以轻松删除它)。我希望这可以帮助其他人在未来。
Here is a sample function that you could use to save the comments. It allows you to edit one key/value pair at a time. I've also added some stuff to format the file nicely based on the way I commonly use the files (You could easily remove that if you want). I hope this might help someone else in the future.
如果评论很重要,那么您唯一的选择可能就是阅读并阅读它。手动保存文件(通过 XmlDocument 或新的 Linq 相关 API)。然而,如果这些评论并不重要,我要么放弃它们,要么考虑将它们嵌入为(尽管是多余的)数据元素。
If comments are critical, it might just be that your only option is to read & save the file manually (via
XmlDocument
or the new Linq-related API). If however those comments are not critical, I would either let them go or maybe consider embedding them as (albeit redundant) data elements.