I have some complex data which is used for application configuration in xml format. I want to keep this xml string in web.config. Is it possible to add a big xml string in web.config and get it in code everywhere?
-
2Sounds like you want to use a `Settings` file? http://msdn.microsoft.com/en-us/library/aa730869(v=vs.80).aspx – Yuck Mar 12 '12 at 12:20
-
nopes I want to have this xml string in web.config only.not in any other settings file or resource file this is my clients requirement. – gmtek Mar 12 '12 at 12:26
-
1The `Settings` values are stored in web.config. The `Settings.settings` file is used by the designer. – jrummell Mar 12 '12 at 12:28
-
1It's your job to convince your client that **they're doing it wrong**. Using `web.config` as a place to dump other XML simply because it's the same type is a poor excuse. There's already infrastructure for working with data in a `Settings` file in a strongly-typed manner. See jrummell's comment. – Yuck Mar 12 '12 at 12:29
3 Answers
If you don't want to write a configuration section handler, you could just put your XML in a custom configuration section that is mapped to IgnoreSectionHandler
:
<configuration>
<configSections>
<section
name="myCustomElement"
type="System.Configuration.IgnoreSectionHandler"
allowLocation="false" />
</configSections>
...
<myCustomElement>
... complex XML ...
</myCustomElement>
...
</configuration>
You can then read it using any XML API, e.g. XmlDocument
, XDocument
, XmlReader
classes. E.g.:
XmlDocument doc = new XmlDocument();
doc.Load(AppDomain.CurrentDomain.SetupInformation.ConfigurationFile);
XmlElement node = doc.SelectSingleNode("/configuration/myCustomElement") as XmlElement;
... etc ...

- 122,218
- 32
- 205
- 338
-
-
Will u plz tell how can I read the "...complex xml..." as a string in my code? – gmtek Mar 13 '12 at 04:29
-
@Idorado, I added an example of reading the custom element using the XmlDocument class. – Joe Mar 13 '12 at 08:20
-
-
@Gulshan: It's nice to reward this user if his answer solved your issue by [marking the answer as accepted](http://meta.stackexchange.com/questions/5234/how-does-accepting-an-answer-work). – JohnDoDo Dec 07 '12 at 17:11
-
2
-
-
1@Boogier, No need to put
in applicationSettings, because, you can define your own Custom Element in the config file this way. – Jan 01 '15 at 10:37
There are several ways of achieving what you want (an XML fragment that is globally and statically accessible to your application code):
The
web.config
is already an XML file. You can write a custom configuration section (as described here) in order to fetch the data from your custom XML.You can encode the XML data (all
<
to<
,>
to>
,&
to&
,"
to"e;
)You can put the XML data in a
<![CDATA[]]>
sectionDon't use
web.config
for this, but aSettings
file as @Yuck commented
That last option is the best one, in terms of ease of development and usage.

- 489,969
- 99
- 883
- 1,009
-
-
Not an array, as far as I am aware, but you _can_ add collections (there is a built in StringCollection, for example). – Oded Mar 12 '12 at 13:11
The configuration sections in web.config support long strings, but the string has to be a single line of text so they can fit into an attribute:
<add name="" value="... some very long text can go in here..." />
The string also can't contain quotes or line breaks or other XML markup characters. The data is basically XML and it has to be appropriately encoded.
For example, if you have XML like this:
<root>
<value>10</value>
</root>
it would have to be stored in a configuration value like this:
<add key="Value" value="<root>
 <value>10</value>
</root>" />
Which kind of defeats the purpose of a configuration element.
You might be better off storing the configuration value in a separate file on the file system and read it from there into a string or XmlDocument etc.

- 17,302
- 14
- 89
- 134