Code Review Stack Exchange is a question and answer site for peer programmer code reviews. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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>
share|improve this question

closed as off-topic by 200_success Oct 24 '16 at 13:51

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "Questions containing broken code or asking for advice about code not yet written are off-topic, as the code is not ready for review. After the question has been edited to contain working code, we will consider reopening it." – 200_success
If this question can be reworded to fit the rules in the help center, please edit the question.

    
Why not use the System.Configuration namespace if your config files are XML? – Mat's Mug Oct 23 '16 at 19:44
1  
It's not clear whether you're asking for a peer review of what you have, or if you're asking for help implementing the else block (which would be off-topic); please edit to clarify. – Mat's Mug Oct 23 '16 at 19:46
    
@Mat'sMug I'm asking for the simplest way to read/write such xml config files. Also if there's a better way to handle cross-sessions settings such as System.Configuration please provide some info as a comment or answer. Thanks – Vad Zelenin Oct 23 '16 at 19:53
    
Please try to write a title that summarizes what your code does, not what you want to get out of a review. Please see How to get the best value out of Code Review - Asking Questions for guidance on writing good question titles. – BCdotWEB Oct 24 '16 at 13:37

Browse other questions tagged or ask your own question.