crauscher
crauscher

Reputation: 6618

.Net application configuration add xml-data

I need to add xml-content to my application configuration file. Is there a way to add it directly to the appSettings section or do I need to implement a configSection?

Is it possible to add the xml to a CDATA element?

Upvotes: 1

Views: 685

Answers (3)

Robert Rossney
Robert Rossney

Reputation: 96750

You can (and I have) save XML in a configuration file as a string. It'll look like this:

<setting name="MyXml" serializeAs="String">
    <value>&lt;foo&gt;Here's my XML.  Read it and weep.&lt;/foo&gt;</value>
</setting>

To retrieve it, you'd do:

string xml = Properties.Settings.Default.MyXml;
XmlDocument d = new XmlDocument();
d.LoadXml(xml);

Upvotes: 0

JohnIdol
JohnIdol

Reputation: 50107

If it goes beyond standard key value pairs you'll have to create your custom section. If it is xml I don't see why you would want to store it as a CDATA blob.

Custom config sections are pretty straightforward to setup - have a look at the accepted answer on this question which covers the topic in detail.

Upvotes: 0

Yogesh
Yogesh

Reputation: 14608

I don't think you can store the xml in the config file without implementing each of it's branches as ConfigurationElement.

You can store the full xml as CDATA though. I think this will help you in the implementation: http://devpinoy.org/blogs/jakelite/archive/2009/03/22/how-to-add-cdata-or-text-in-a-configurationelement.aspx

Upvotes: 1

Related Questions