自定义app.config部分C#



我在使用app.config上有点迷失...我一直在使用.ini文件,但是现在我想更改...这就是我的方式。Ini看起来

[Clients]
1 = Client1
2 = Client 2
[Client1]
Employees = 4
1 = Employee1
2 = Employee2
3 = Employee3
4 = Employee4
[Client2]
Employees = 3
1 = Employee1
2 = Employee2
3 = Employee3

然后,我在循环中使用了一些,首先是我在[客户端]中搜索的客户,我搜索它有多少员工,然后,对于每个员工,我都会做事...

如何将此.ini转换为类似的东西

<Clients>
 <name = "client1">
  <Employees>
    <name = "employee1" />
    <name = "employee2" />
    <name = "employee3" />
    <name = "employee4" />
  </Employees>
 <name = "client2">
  <Employees>
    <name = "employee1" />
    <name = "employee2" />
    <name = "employee3" />
  </Employees>
</Clients>

然后使用该app.config

使用循环进行操作

感谢建议。

更新我已经尝试过此linq到XML代码

XDocument doc = new XDocument(
         new XDeclaration("1.0", "utf-8", "yes"),
         new XComment("Configuración Checkcenter"),
         new XElement("Entidad",
             new XAttribute("nombre",cmbEntidad.Text),
             new XElement("Servicios",
                 new XElement("nombre", cmbServicio.Text))));
    doc.Save("settings.xml");

它有效,将我返回:

<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<!--My Settings-->
<Client name="Client1">
  <Employees>
    <name>Employee1</name>
  </Employees>
</Entidad>

但它总是在员工中取代我的名字...如果不存在新项目,该如何处理?我的意思是,如果不存在,将新项目添加到员工的新项目中。

为什么要使用app.config?app.config作为名称建议用于存储配置设置。

最好的事情是使用XML文档。这将使您的数据与配置设置分开。从XML文件中阅读后,您可以将LINQ到XML查询这些数据。

正如其他人所说的那样,这样的数据应该易于消耗存储(即db,xml)但是,如果您必须使用app.config文件。您必须查看system.configuration.configurationsection。

msdn链接

专注于配置,配置和配置ElementCollection类。

最新更新