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)

8
  • 1
    the attribute name is different and so is the node name, so they aren't duplicates. Commented Jun 5, 2013 at 12:58
  • 1
    there's great tool to create custom configuration sections: configuration section designer: csd.codeplex.com Commented Jun 5, 2013 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? Commented Jun 5, 2013 at 13:07
  • @Giedrius Thanks, I'll definitely will use it after learning doing it with hands Commented Jun 5, 2013 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 ;-) Commented Jun 5, 2013 at 13:16

4 Answers 4

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.

Sign up to request clarification or add additional context in comments.

2 Comments

Yes, even better. global section is my unnecessary invention =)
@astef Yeah..why to reinvent the wheel
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.

Comments

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)

2 Comments

ConfigurationSections are already created (ReplicationConfigSection and ProcessingConfigSection). I can't understand, how it is related to string duplicates in xml.
xsd is not usable as System.configuration file, isn't it?
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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.