XML File as Options settings file, one file different apps how to do

I have 2 independent DLLs that I’ll include as part of a solution. Each one can be used without the other and I’m now building #3.

The challenge I have is the each has a global configuration and each has a workstation(PC) configuration file.

Now the Station configuration is best left as its own file but the global configuration file well that’s my new issue.

In each DLL(Module) I setup a logging feature. The setting for this is in the global Config file. In addition each DLL has its own additional settings.

First DLL config

<GlobalConfigData xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <LogFilesUNC>Logs</LogFilesUNC>
  <AllowDBMaintenance></AllowDBMaintenance>
  <DBKeepDays></DBKeepDays>
  <ReportsUNC></ReportsUNC>
</GlobalConfigData>

Second DLL Config File:

<GlobalConfigData xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <LogFilesUNC>Logs</LogFilesUNC>
  <DBServerURI></DBServerURI>
  <DBServerPort></DBServerPort>
</GlobalConfigData>

As you can see the LogFilesUNC is in both files. What I want is to read and process a single config file such as:

<GlobalConfigData xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <LogFilesUNC>Logs</LogFilesUNC>
</GlobalConfigData>

<DLL1ConfigData xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <AllowDBMaintenance></AllowDBMaintenance>
  <DBKeepDays></DBKeepDays>
  <ReportsUNC></ReportsUNC>
</DLL1ConfigData>

<DLL2ConfigData xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <DBServerURI></DBServerURI>
  <DBServerPort></DBServerPort>
</DLL2ConfigData>

I want to keep thing such that DLL1 does not need to know about DLL2 in my code.

Here is how I read the files now:

//First define the file structure:
using System;
using System.Xml.Serialization;

namespace ReportMgr2022.Models
{
  [Serializable(), XmlRoot()]
  public class GlobalConfigData
  {
    public string LogFileUNC { get; set; } = @"Logs";
    public bool AllowDBMaintenance { get; set; } = false;
    public int DBKeepDays { get; set; } = 90;
  }
}

And the code that processes the file:

namespace ReportMgr2022
{
  internal class ConfigOptionsv2
  {
    private readonly string _CfgFQN;
    public string Message { get; private set; }

    public ConfigOptionsv2(string configFileFQN)
    {
      _CfgFQN = configFileFQN;
    }

    public bool ValidateConfigFile()
    {
      GlobalConfigData oOptions = null;
      Message = string.Empty;
      try
      {
        oOptions = LoadFromXmlFormat();
      }
      catch (Exception) { } //Error is ignorable

      if (oOptions == null)
      {
        oOptions = new GlobalConfigData();
        SaveAsXmlFormat(oOptions);
      }

      return string.IsNullOrWhiteSpace(Message);
    }

    public bool SaveAsXmlFormat(GlobalConfigData oOptions)
    {
      XmlSerializer xmlformat = new XmlSerializer(typeof(GlobalConfigData));

      try
      {
        using (Stream fstream = new FileStream(_CfgFQN, FileMode.Create, FileAccess.Write, FileShare.None))
        {
          xmlformat.Serialize(fstream, oOptions);
        }
      }
      catch (Exception ex)
      {
        Message = ParseException(ex);
        return false;
      }

      return true;
    }

    public GlobalConfigData LoadFromXmlFormat()
    {
      XmlSerializer xmlformat = new XmlSerializer(typeof(GlobalConfigData));
      GlobalConfigData oOptionsFromDisk = null;

      try
      {
        using (Stream fstream = File.OpenRead(_CfgFQN))
        {
          oOptionsFromDisk = (GlobalConfigData) xmlformat.Deserialize(fstream);
        }
      }
      catch (Exception ex)
      {
        throw new GlobalConfigException("ConfigOptionsv2.LoadFromXmlFormat", ParseException(ex), ex);
      }
      return oOptionsFromDisk;
    }

  }

  [Serializable]
  internal class GlobalConfigException : Exception
  {

    public GlobalConfigException() { }

    public GlobalConfigException(string message) : base(message) { }

    public GlobalConfigException(string source, string message, Exception inner) : base(message, inner) { Source = source; }

    public GlobalConfigException(string message, Exception innerException) : base(message, innerException) { }

    protected GlobalConfigException(SerializationInfo info, StreamingContext context) : base(info, context) { }
  }

}

I know the load and save will need change but the issue I have seems to be when DLL1 updates the DLL2 data is wiped out. Based on the code that makes sense. Is ther a way to do this?

My thought is that I would need to read in the entire file any time I save and then modify the individual elements for the DLL and then write the entire file back out.

Or should I create a “Global config” for just the log file and keep everything else in its own file?

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật