Variables in XML Configuration

I am trying to use the xml configuration file in my project. Now it looks like this:

<?xml version="1.0" encoding="utf-8" ?> <configuration> <configSections> <section name="replication" type="Project.Replication.ReplicationConfigSection, Project.Replication" /> <section name="processing" type="Project.Processing.ProcessingConfigSection, Project.Processing" /> </configSections> <replication> <streams> <stream name="STREAM_DATA_14360" /> </streams> </replication> <processing dataStream="STREAM_DATA_14360" /> </configuration> 

It works fine, but I'm confused with its duplicates ( "STREAM_DATA_14360" ).

Can you remind me how to create variables in XML or something so that data reuse is acceptable in the application configuration?

UPDATE:

In real life, my configuration has many more sections. There is a meaning that occurs in many of these sections: STREAM_DATA_14360 . Therefore, I want to be able to change this value only in one place of the configuration file, and in other places use a link to it.

The speed of configuration changes is the first reason for this.

The file size is a second because the values ​​can be huge: STREAM_INFO_FUTURE_SESSION_CONTENTS_12421 (these are the names of third-party developers)

+4
source share
4 answers

You can simply add this value to <appSettings> and access it as you say.

You can do this as shown below:

 <appSettings> <add key="StreamName" value="STREAM_DATA_14360"/> </appSettings> 

In the code, you can access it, as shown below:

  string streamName = ConfigurationManager.AppSettings["StreamName"]; 

Be sure to add a reference to the System.Configuration assembly before using it.

+1
source

XML does not have built-in extension macros or templates - any script will require you to complete the preprocess step or get code that reads the configuration involved in the value replacement.

If these names are not edited, it seems that a simple search / replace will solve the problem without worrying about false positives.

You can put something along with T4 templates as a preprocessor, is it really worth it how often do you plan to modify this file.

You also need to change the mechanism for converting web.config to perform replacements, but you may have to write the hosting code for the XDT engine depending on how your configuration file is configured.

+1
source

In addition to using external code that may (or may not) make your life easier, you can define your own classes that inherit from ConfigurationSection , where you define and encapsulate key / value pairs and use the ConfigurationProperty attribute.

See http://msdn.microsoft.com/en-us/library/2tw134k3.aspx for more information on how to: create custom configuration sections using ConfigurationSection.

EDIT: you can make links in xsd (check here )

0
source

Thank you for your responses. I agree with Mark, there is no support for variables or references in XML. But in my case, a much simpler solution. Now I feel stupid, but hope this helps another slow cause.

 <?xml version="1.0" encoding="utf-8" ?> <configuration> <configSections> <section name="global" type="Project.GlobalConfigSection, Project" /> <section name="replication" type="Project.Replication.ReplicationConfigSection, Project.Replication" /> <section name="processing" type="Project.Processing.ProcessingConfigSection, Project.Processing" /> </configSections> <global> <streamNames> <streamName name="STREAM_DATA_14360" id="1"/> </streamNames> </global> <replication> <streams> <stream nameId="1" /> </streams> </replication> <processing dataStreamId="1" /> </configuration> 

Corollary: you need to edit the code to use the global section as the source of all long names

Benefit: quick rename, reuse of values

0
source

Source: https://habr.com/ru/post/1484636/


All Articles