user1062142
user1062142

Reputation: 119

Need Simple way to load some Config Settings for App

Visual Studio 2010 C# .Net 4.0 Windows Forms App

I have several configuration setting that I would like to store in an XML file.

I want to load values from the XML file for: Background Color, Input Port, TimeOut Seconds, and Region This is a very specific project and it will have just to 4 parameters in total. I do not need this to be flexible. I need it simple. Have been trying for a week now.. all examples I found are not working for my simple file.

My Xml File is basic:

<?xml version="1.0"?>
<appsettings>
    <bgcolor>BLUE</bgcolor>
    <inputport>1</inputport>
    <timeout>50</timeout>
    <region>USA</region>
</appsettings>

Next, I would like to load the XML file at startup and store each value in my string vars. This is where i am stuck. I know there must be a very simple way such as:\

private void form1_Load(object sender, EventArgs e)
{
    XDocument XDoc = XDocument.Load(@"C:\rm\rmdemo_18\Rmocd001.xml");
    String FormColor = (String)XDoc.Root.Element("appsettings").Element("bgcolor");
    String Region = (String)XDoc.Root.Element("appsettings").Element("region");
    textLocalization.text = Region;
    textFormColor.text = FormColor;
          etc.....
}

Any help or direct would be appreciated please. Thank you.

Upvotes: 0

Views: 412

Answers (1)

ArunJohney
ArunJohney

Reputation: 91

You could use the ConfigurationManager class if you add it to you app.config file.

 ConfigurationManager.AppSettings["bgcolor"]

If you really need to use Linq to XML you can use.

var bgColor =  XDoc.Root.Descendants("bgcolor").First().Value

Upvotes: 2

Related Questions