Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add a xml in web.config?

Tags:

c#

.net

asp.net

wcf

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?

like image 484
Gulshan Avatar asked Mar 12 '12 12:03

Gulshan


People also ask

Is web config an XML file?

The Web. config file must be a well-formed XML document and must have a format similar to the %SystemRoot%\Microsoft.NET\Framework\%VersionNumber%\CONFIG\Machine.

What is web config XML?

web. config file is an XML-based configuration file used in ASP. NET-based applications to manage various settings that are concerned with the configuration of our website. In this way, we can separate our application logic from configuration logic.


1 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 ...
like image 93
Joe Avatar answered Oct 14 '22 09:10

Joe