0

I'm new to C# and would like to create a WPF app with xml config file. I have a certain setting file written by using System.Xml.Linq;.

And I'd like to write else block to read data from the file if it exists. I'm a static struct instead of class.

public struct Settings {
    public static int firstParam { get; set; }
    public static int secondParam { get; set; }

    public static void Initialize()
    {
        loadFromFile();
    }

    public static void loadFromFile()
    {
        if (!File.Exists(@"./config.xml"))
        {
             //Write file with default values                 
             setDefault();

             XElement fP = new XElement("firstParam");
             XElement sP = new XElement("secondParam");
             fp.Value = firstParam.ToString();
             sp.Value = secondParam.ToString();
             XElement root = new XElement("settings", fp, sp);
             new XDocument(root).Save(@"./config.xml");
        }
        else
        {
             //Read the existed file and set 
        }
    }

    private static void setDefault()
    {
        firstParam = 5;
        secondParam = 5;
    }
}

I found that there's a lot of outdated info about this on the StackOverFlow. Also there are XML tutorials that are teaching how to read bunch of same xml objects via foreach loops or Linq and I feel like they're not suitable for my issue. So there's a question I'd like to ask:

Is there a more suitable way in 2016 to read/write xml settings files like this one:

<?xml version="1.0" encoding="utf-8"?>
<settings>
  <firstParam>5</firstParam>
  <secondParam>5</secondParam>
</settings>
Vadym
  • 548
  • 2
  • 8
  • 21
  • 1
    Why you dont want to use ```app.config``` file? – tym32167 Oct 23 '16 at 18:46
  • Could you provide some info about this method. Here in comments section or as an answer will you. Thanks – Vadym Oct 23 '16 at 18:49
  • you can read anwer from here http://stackoverflow.com/questions/13043530/what-is-app-config-in-c-net-how-to-use-it – tym32167 Oct 23 '16 at 18:51
  • @tym32167 as mentioned in the answer there: *Frequently changing the *.config files is usually not a good idea, but it sounds like you only want to perform one-time setup.* and I want my app to have a settings section where user will be able to change settings as many times as needed. Is `app.config` a good solution for this? – Vadym Oct 23 '16 at 19:09
  • 2
    Another option is [User Settings](https://msdn.microsoft.com/en-us/library/bb397750(v=vs.110).aspx) – Crowcoder Oct 23 '16 at 19:11
  • @Crowcoder seems like it fits my needs. But it's in Windows Forms topic. Is it a good to mix this technologies? – Vadym Oct 23 '16 at 19:21
  • @VadZelenin if you want to store big amount of settings or big amount of data (I worked with WPF projects where settings was ~ 10MB) or you planning a bunch of read/write operations, so, appsettings - bad idea. But, if you want to store a couple of settings with small data, its ok. – tym32167 Oct 23 '16 at 19:22
  • Yes, I use it myself in WPF. – Crowcoder Oct 23 '16 at 19:24

0 Answers0