4

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)

astef
  • 8,575
  • 4
  • 56
  • 95
  • 1
    the attribute name is different and so is the node name, so they aren't duplicates. – Wim Ombelets Jun 05 '13 at 12:58
  • 1
    there's great tool to create custom configuration sections: configuration section designer: https://csd.codeplex.com/ – Giedrius Jun 05 '13 at 13:00
  • @WimOmbelets So, if I have dozens of identical strings in different sections - all of them are not duplicates? OK, can you tell me how to perform fast change of this "NOT duplicates", in one place of xml file? – astef Jun 05 '13 at 13:07
  • @Giedrius Thanks, I'll definitely will use it after learning doing it with hands – astef Jun 05 '13 at 13:12
  • as long as the combination of node name and argument key is unique, then no they are not duplicates. Even if the node name and argument key appear multiple times but have different parent nodes that have unique name (and/or) argument keys, they're still not duplicates ;-) – Wim Ombelets Jun 05 '13 at 13:16
  • @WimOmbelets I am a novice in XML, so I'm not arguing about definitions. I just need to solve my problem. Let's call it "argument duplicates". Can argument duplicates be eliminated by defining them in one place and using a variable? – astef Jun 05 '13 at 13:30
  • Your question isn't clear to me at the moment, sorry. Could you maybe edit your question to make "Can you remind me, how to create variables in XML or something for data reusing to be acceptable in application configuration?" a bit clearer? Thanks. – Wim Ombelets Jun 05 '13 at 13:35
  • @WimOmbelets I've updated post, hope it helps – astef Jun 05 '13 at 13:50

4 Answers4

1

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.

Mark Brackett
  • 84,552
  • 17
  • 108
  • 152
1

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.

Bhushan Firake
  • 9,338
  • 5
  • 44
  • 79
0

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)

Community
  • 1
  • 1
Wim Ombelets
  • 5,097
  • 3
  • 39
  • 55
  • `ConfigurationSections` are already created (`ReplicationConfigSection` and `ProcessingConfigSection`). I can't understand, how it is related to string duplicates in xml. – astef Jun 05 '13 at 13:21
  • xsd is not usable as System.configuration file, isn't it? – astef Jun 05 '13 at 14:06
0

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

astef
  • 8,575
  • 4
  • 56
  • 95