在代码中编辑 .config 文件

发布于 2024-11-25 05:01:17 字数 1377 浏览 1 评论 0原文

我正在使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

渔村楼浪 2024-12-02 05:01:17

我建议创建封装此信息(服务器)和功能(添加服务器)的自定义对象,然后将它们序列化为与 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文