0

I have configuration look like this and I don't know how to read, I want to get value if I choose product or preview?

<configuration> 

  <environment name="product">
     <connectionString> connection string</connectionString>
     <logPath>C:\*****</logPath>
     <errorLogPath>C:\*****</errorLogPath>
     <ProcessesNumber>5</ProcessesNumber>
      <sendAtOnce>100</sendAtOnce>
     <restInterval>30000</restInterval>
     <stopTime>30000</stopTime>
 </environment>

 <environment name="preview">
    <connectionString> connctionstring </connectionString>
    <logPath>C:\*****</logPath>
    <errorLogPath>C:\*****</errorLogPath>
    <ProcessesNumber>5</ProcessesNumber>
    <sendAtOnce>100</sendAtOnce>
    <restInterval>30000</restInterval>
   <stopTime>30000</stopTime>
 </environment>

</configuration>

How can I read this in my debugging?

CodeCaster
  • 147,647
  • 23
  • 218
  • 272
vegas red
  • 259
  • 4
  • 12
  • 2
    Does [this answer](http://stackoverflow.com/a/3994081/266143) help? Using that, a `web.$(Configuration).config` will be created on every build, allowing you to have separate debug- and release-configs. – CodeCaster Apr 15 '13 at 11:02

2 Answers2

1

It's quite easy using LINQ

This should help you

class Configuration
{
    public string connectionString { get; set; }
    public string logPath { get; set; }
    public string errorLogPath { get; set; }
    public int ProcessesNumber { get; set; }
    public int sendAtOnce { get; set; }
    public int restInterval { get; set; }
    public int stopTime { get; set; }
}

static void Main(string[] args)
{
    try
    {
        XDocument doc = XDocument.Load("config.xml");
        string conftype = "product";

        var configuration = (from config in doc.Elements("configuration").Elements("environment")
                             where config.Attribute("name").Value.ToString() == conftype
                             select new Configuration
                             {
                                 connectionString = config.Element("connectionString").Value.ToString(),
                                 logPath = config.Element("logPath").Value.ToString(),
                                 errorLogPath = config.Element("errorLogPath").Value.ToString(),
                                 ProcessesNumber = int.Parse(config.Element("ProcessesNumber").Value.ToString()),
                                 sendAtOnce = int.Parse(config.Element("sendAtOnce").Value.ToString()),
                                 restInterval = int.Parse(config.Element("restInterval").Value.ToString()),
                                 stopTime = int.Parse(config.Element("stopTime").Value.ToString()),
                             }).First();
    }
    catch (Exception e)
    {
        Console.WriteLine(e.ToString());
    }
}
sarat
  • 10,512
  • 7
  • 43
  • 74
0

In C# I recommend you to use Linq to XML. It is in the standard .net framework (3.5), and it help you to load the XML, and to read every node and attribute of it easily.

Rafa
  • 2,328
  • 3
  • 27
  • 44