ConfigurationManager中可以保留在保存XML注释()?

编程入门 行业动态 更新时间:2024-10-27 11:29:02
本文介绍了ConfigurationManager中可以保留在保存XML注释()?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我写了一个小工具,它可以让我改变了另一个应用程序的App.config文件的简单AppSetting,然后保存更改:

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);

这工作得很好,除了一个副作用。 新保存的config文件丢失所有的原始XML注释,但只有AppSettings的区域内。是否有可能保留原始配置文件的AppSettings区XML注释?

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?

的 下面是完整的源代码的引擎收录,如果你想快速编译并运行它。 的

推荐答案

我跳进Reflector.Net,看着反编译源代码这个类。简短的回答是否定的,它不会保留意见。微软写的类的方法是生成的配置类属性的XML文档。由于意见不配置类展现出来,他们不让它回到XML。

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.

和什么使这更糟糕的是,微软密封所有这些类,所以你不能派生新类,并插入自己的实现。你唯一的选择是将评论appSettings部分之外,或使用的XmlDocument 或的XDocument 类来解析配置文件来代替。

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 or XDocument classes to parse the config files instead.

对不起。这是微软只是没有为计划的边缘情况。

Sorry. This is an edge case that Microsoft just didn't plan for.

更多推荐

ConfigurationManager中可以保留在保存XML注释()?

本文发布于:2023-11-13 14:09:50,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1584527.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:注释   ConfigurationManager   XML

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!