Question

I'm trying to use xml configuration file in my project. Now it looks like:

<?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 OK, but I'm confused with duplicates in it ("STREAM_DATA_14360").

Can you remind me, how to create variables in XML or something for data reusing to be acceptable in application configuration?

UPDATE:

In real life my configuration has much more sections. There is a value, which apeears in many of this sections: STREAM_DATA_14360. So I want to be able to change this value only in one place of config file, and in other places to use reference to it.

Speed of changing configuration - is the first reason for it.

Size of a file is a second, because values can be huge: STREAM_INFO_FUTURE_SESSION_CONTENTS_12421 (that is third-party names)

Was it helpful?

Solution

You can simply add this value in <appSettings> and access it as you are saying.

You can do this as below:

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

In the code, you can access it as below:

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

Make sure to add reference to System.Configuration assembly before using this.

OTHER TIPS

XML doesn't have any native expansion macros or templating - any scenario would require that you do a preprocess step or have the code that reads the config involved in substituting the value.

If those aren't redacted names though, it seems a simple search/replace would solve the problem without much of a concern on false positives.

You could put something together with T4 templates as a preprocessor, whether that's worth it really depends on how often you expect to modify this file.

It should also be possible to shoehorn the web.config transformation engine into doing the replacements, but you may have to write some hosting code for the XDT engine depending on how your config file is setup.

Apart from using external code that might (or might not) facilitate your life, you can define your own classes that inherit from ConfigurationSection, wherein you define and encapsulate your key/value pairs and use the ConfigurationProperty attribute.

Have look at http://msdn.microsoft.com/en-us/library/2tw134k3.aspx for more info on How to: Create Custom Configuration Sections Using ConfigurationSection.

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

Thanks for your answers. I agree with Mark, there's no support of variables or references in XML. But, in my case there's much simpler solution. I feel stupid now, but hope that it will help another slowpoke too.

<?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>

Consequence: need to edit code to use global section as a source of all long names

Advantage: fast renaming, reusability of values

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top