Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to pull configuration settings from XML file in Startup.cs

I have a startup.cs file and i want to pull configurations from XML file rather than appsetings.json file. Is it possible with ASP.NET Core MVC?

like image 836
InspireMe Avatar asked Oct 14 '16 06:10

InspireMe


People also ask

How read app config file in C# Windows application?

Add the App. config file to your project. After creating a . NET Framework project, right-click on your project in Solution Explorer and choose Add > New Item. Choose the Application Configuration File item and then select Add.

Is XML a config file?

The config. xml file consists of a series of XML elements. The Domain element is the top-level element, and all elements in the Domain are children of the Domain element. The Domain element includes child elements, such as the Server, Cluster, and Application elements.

How to use XML configuration in Spring Boot application?

We will use this XML configuration in our spring boot application. XML file is imported in configuration file using @ImportResource with @Configuration. In our main class we are using @SpringBootApplication annotation. @SpringBootApplication is the combination of @Configuration, @EnableAutoConfiguration and @ComponentScan annotations.

What is a settings XML file?

It's similar to a pom.xml file but is defined globally or per user. Let's explore the elements we can configure in the settings.xml file. The main, settings element of the settings.xml file, can contain nine possible predefined child elements: 2.1.

How to set the values inside the configuration file?

There are different ways to set the values inside the configuration file and read their values, which are based on the defined keys. We define those values inside the configuration section, which might be needed to make it more secure. It can be some secret keys or the value, which should be received frequently.

How to create a REST web service using XML in Java?

We will create a REST web service with XML configuration. We will import our XML file in java configuration. We need to use @ImportResource with @Configuration in our spring boot application. We can keep our XML files in project classpath. Here we will create a spring boot web application that will work as REST web service.


Video Answer


1 Answers

If you want to use an appsettings.xml instead you can refer to my blog article here.

Quote from the article:

To use XML files, you need to add this NuGet package: Microsoft.Extensions.Configuration.Xml. I added the 1.0.0 to my project.json and added a appsettings.xml file.

<configuration> <MySettings> <SomeSetting>Test</SomeSetting> <Another>true</Another> </MySettings> </configuration>

I then added it as part of configuration like this:

.AddXmlFile("appsettings.xml", optional: true, reloadOnChange: true)

like image 51
juunas Avatar answered Dec 13 '22 00:12

juunas