在代码中编辑 .config 文件
我正在使用 WPF 和 C# 编写一个程序,该程序有一个“app.config”文件,该文件存储多个服务器的名称和位置。程序动态地读取此配置文件,并将其显示在 UI 中(以及其他内容)。
我试图包含一种将新服务器添加到配置文件中的前端方法,以便用户可以输入所需服务器的属性,并且我的程序将其添加到配置文件中。
我的配置文件本质上是这样的:
<ServerConfig>
<Environment name="PROD">
<Server host="h1" share="s1" source="x1"/>
<Server host="h6" share="s1" source="x1"/>
<Server host="h7" share="s1" source="x1"/>
<Server host="h8" share="s1" source="x1"/>
<Server host="h155" share="s1" source="x1"/>
</Environment>
<Environment name="DEV">
<Server host="h2" share="s2" source="x2"/>
<Server host="h55" share="s1" source="x1"/>
<Server host="h115" share="s1" source="x1"/>
</Environment>
<Environment name="QA">
<Server host="h3" share="s3" source="x3"/>
<Server host="h46" share="s1" source="x1"/>
<Server host="h15" share="s1" source="x1"/>
<Server host="h2" share="s1" source="x1"/>
<Server host="h234" share="s1" source="x1"/>
<Server host="h6" share="s1" source="x1"/>
<Server host="h146" share="s1" source="x1"/>
</Environment>
</ServerConfig>
我希望用户能够输入主机、共享、源和环境,并且我需要找到一种方法将该行 Xml 代码放置在正确的位置。
有没有办法搜索具有特定“名称”的标签(例如搜索“QA”环境)并在其后添加行? XmlWriter/XmlSerializer 似乎只能从 Xml 文件的开头开始写入,而不能在写入之前对其进行梳理。
我很感激任何帮助。
I'm writing a program using WPF and C# that has an "app.config" file which stores the name and location of several servers. The program dynamically reads from this config file, and displays it(among other things) in a UI.
I'm trying to include a front end way to add new servers to the configuration file, so that the user can enter the properties of the server needed, and my program adds it to the config file.
My config file essentially looks something like this:
<ServerConfig>
<Environment name="PROD">
<Server host="h1" share="s1" source="x1"/>
<Server host="h6" share="s1" source="x1"/>
<Server host="h7" share="s1" source="x1"/>
<Server host="h8" share="s1" source="x1"/>
<Server host="h155" share="s1" source="x1"/>
</Environment>
<Environment name="DEV">
<Server host="h2" share="s2" source="x2"/>
<Server host="h55" share="s1" source="x1"/>
<Server host="h115" share="s1" source="x1"/>
</Environment>
<Environment name="QA">
<Server host="h3" share="s3" source="x3"/>
<Server host="h46" share="s1" source="x1"/>
<Server host="h15" share="s1" source="x1"/>
<Server host="h2" share="s1" source="x1"/>
<Server host="h234" share="s1" source="x1"/>
<Server host="h6" share="s1" source="x1"/>
<Server host="h146" share="s1" source="x1"/>
</Environment>
</ServerConfig>
I want the user to be able to input a host, share, and source, and environment, and I need to find a way to place that line of Xml Code in the proper place.
Is there a way to search for a tag with a certain "name" (such as searching for the 'QA' environment) and adding the line after that? XmlWriter/XmlSerializer seems to be able to only write from the beginning of an Xml file, not comb through it before writing.
I appreciate any help.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我建议创建封装此信息(服务器)和功能(添加服务器)的自定义对象,然后将它们序列化为与 web.config 分开的 XML 文件。这将您的自定义设置全部保存在一个位置,并避免干扰标准配置文件,我认为这对于运行时未更改的设置很有用。我已经为一个项目这样做过,效果很好。
I suggest creating custom objects that encapsulate this information (servers) and functionality (adding servers), then serializing those a an XML file separate from the web.config. This keeps your custom settings all in one place, and avoids meddling in the standard config file which I see as being useful for settings that are not changed at runtime. I've done this for one project and it works well.