Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9221423
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T03:34:35+00:00 2026-06-18T03:34:35+00:00

I have this class: using System.IO; using System.Xml.Serialization; namespace ssscc.Settings { public class AppSettings

  • 0

I have this class:

using System.IO;
using System.Xml.Serialization;

    namespace ssscc.Settings
    {
      public class AppSettings
      {
        private string _companyName;
        public string CompanyName
        {
          set { _companyName = value; }
          get
          {
            if (string.IsNullOrWhiteSpace(_companyName))
            {
              LoadSettings();
            }
            return _companyName;
          }
        }

        private string _companyPhone;
        public string CompanyPhone
        {
          set
          {

            _companyPhone = value;
          }
          get
          {
            if (string.IsNullOrWhiteSpace(_companyPhone))
            {
              LoadSettings();
            }
            return _companyPhone;
          }
        }

        private string GetSettingsFile()
        {
          var exePath = System.Windows.Forms.Application.StartupPath;
          var sharedDirectory = Path.Combine(exePath, "shared");
          var settingsDirectory = Path.Combine(sharedDirectory, "settings");
          var settingsFile = Path.Combine(settingsDirectory, "ssscc.xml");

          if (!Directory.Exists(sharedDirectory))
          {
            Directory.CreateDirectory(sharedDirectory);
          }

          if (!Directory.Exists(settingsDirectory))
          {
            Directory.CreateDirectory(settingsDirectory);
          }

          return settingsFile;
        }

        internal void SaveSettings(AppSettings settings)
        {
          var serializer = new XmlSerializer(typeof(AppSettings));
          using (var stream = File.OpenWrite(GetSettingsFile()))
          {
            serializer.Serialize((Stream) stream, (object) settings);
          }
        }

        internal void LoadSettings()
        {
          if (!File.Exists(GetSettingsFile()))
          {
            return;
          }

          var serializer = new XmlSerializer(typeof(AppSettings));
          using (var stream = File.OpenRead(GetSettingsFile()))
          {
            var appsetting = (AppSettings) serializer.Deserialize(stream);
            CompanyPhone = appsetting.CompanyPhone;
            CompanyName = appsetting.CompanyName;
          }
        }

      }
    }

My question is about this code:

   var appsetting = (AppSettings) serializer.Deserialize(stream);
    CompanyPhone = appsetting.CompanyPhone;
    CompanyName = appsetting.CompanyName;

I am pretty sure there is a way to return the appsettings directly to the class that contains the method so I do not have to loop through each property such as this:

    CompanyPhone = appsetting.CompanyPhone;
    CompanyName = appsetting.CompanyName;

Can I assign the properties directly without having to maintain this code?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T03:34:36+00:00Added an answer on June 18, 2026 at 3:34 am

    You are getting a new instance of AppSettings while deserializing from file. You may use it, can’t you? Try to replace LoadSettings with a static factory method like this:

    internal static AppSettings GetInstance()
    {
        if (!File.Exists(GetSettingsFile()))
            return null;
    
        var serializer = new XmlSerializer(typeof(AppSettings));
        using (var stream = File.OpenRead(GetSettingsFile()))
            return (AppSettings)serializer.Deserialize(stream);
    }
    

    while to save your settings, you have no need to pass the settings object as an argument. I guess the following code should do the job:

    internal void SaveSettings()
    {
        var serializer = new XmlSerializer(typeof(AppSettings));
        using (var stream = File.OpenWrite(GetSettingsFile()))
            serializer.Serialize((Stream)stream, this);
    }
    

    Use the factory GetInstance method to initialize settings (well, as an example):

    var s = AppSettings.GetInstance();
    if (s == null)
    {
        s = new AppSettings
        {
            CompanyName = "MyCompany",
            CompanyPhone = "######"
        };
        s.SaveSettings();
    }
    

    P.S.: if properties getters and setters have no additional logic (LoadSettings method no longer exists), you could use auto-properties:

    public string CompanyName { get; set; }
    
    public string CompanyPhone { get; set; }
    

    and GetSettingsFile may be declared as static, as it does not operate any of the instance class members:

    private static string GetSettingsFile()
    {
        //...
        return settingsFile;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this class: using System; using System.Collections.Generic; using System.Runtime.Serialization; namespace Grouping { [Serializable]
I have this simple example: using System; using System.Collections.Generic; namespace ConsoleApplication1 { class Program
I have this in furniture.h: #include <iostream> #include <string> using namespace std; class Furniture
I am creating an XML file using the System.Xml.Serialization module. I have a class
I have to realize a serializazion/deserialization class and i'm using System.Xml.Serialization . I have
I have a base class Parent like this: using System; using System.Collections.Generic; using System.Text;
So we have our J2EE application using Log4j like this public class CustomerController {
I'm using MVC3 and I have a model like this: public class Foo {
Inside a class, I have the following piece of code: /// <remarks/> [System.Xml.Serialization.XmlElementAttribute(Errors, typeof(ErrorsType))]
Using Dozer to map two objects, I have: /** /* This first class uses

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.