简体   繁体   English

自定义app.config部分C#

[英]Custom app.config sections C#

I'm a bit lost on using app.config... I've been using .ini files all the time but now I want to change... This is how my .ini looks 我对使用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

Then I was using some for loops, first I search [Clients], inside that For client, I search how many employees does it have, and then, For each employee I do things... 然后我使用了一些for循环,首先搜索[Clients],在其中搜索For Client,然后搜索有多少员工,然后为每位员工做事...

How can I convert this .ini into something like this 我如何将这个.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>

And then do things with loops using that app.config 然后使用该app.config处理循环

Thanks in advice. 感谢您的建议。

UPDATE I've tried this linq to xml code 更新我已经尝试过此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");

It works, returns me this: 它有效,给我以下信息:

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

But it always replace my name in employees... How can I handle it to add new item if it doesn't exists? 但是它总是替换员工的我的名字...如果不存在该如何添加新项? I mean, add new item to Employees for a client if it doesn't exist. 我的意思是,如果客户不存在,请将新项目添加到“雇员”中。

Why do you want to use app.config? 为什么要使用app.config? App.config as the name suggest should be used to store configuration settings. 建议使用App.config作为名称来存储配置设置。

Best thing to do is to use an Xml document. 最好的办法是使用Xml文档。 This will keep your data separate from configuration settings. 这将使您的数据与配置设置分开。 You can use Linq to XML to query these data once you read from XML file. 从XML文件读取后,可以使用Linq to XML查询这些数据。

As the others have said, data like this should be on an easy to consume storage (ie DB, XML) But, if you must use the app.config file for this. 正如其他人所说,这样的数据应该存储在易于使用的存储(即DB,XML)上,但是,如果必须为此使用app.config文件。 You would have to look at the System.Configuration.Configurationsection. 您将不得不查看System.Configuration.Configuration部分。

MSDN Link MSDN链接

Focus on the ConfigurationSection, ConfigurationElement and ConfigurationElementCollection classes. 专注于ConfigurationSection,ConfigurationElement和ConfigurationElementCollection类。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM