What is the correct way to load values ​​from a file during application startup?

I have certain values ​​stored in an XML file. These values ​​must be read at application startup time and must be accessible to all other classes. The xml structure is as follows:

<Values>
    <Tag1>Value1</Tag1>
    <Tag2>Value2</Tag2>
    <Tag3>Value3</Tag3>
</Value>

They should be read only once and this also during startup. If, after starting the application and reading all the values, the values ​​change in any way, then it should not affect the values ​​that the application has read. I want to say that this should not be when the class object requires a value at runtime, every time the xml is read and the value is retrieved.

My first question is: Is this a good practice?

If yes, then

What is the best way to do this in C #?

, , , , .

static class Program
{
    /// <summary>
    /// The main entry point for the application.
    /// </summary>
    [STAThread]
    static void Main()
    {
        Application.EnableVisualStyles();
        Application.SetCompatibleTextRenderingDefault(false);
        string strPath = "";
        XmlDocument doc = new XmlDocument();
        doc.Load(strPath);
        Reader.ReadXml(doc.SelectSingleNode("/Values/Tag1").InnerText,doc.SelectSingleNode("/Values/Tag2").InnerText,doc.SelectSingleNode("/Values/Tag3").InnerText);
        Application.Run(new class1());
    }
}

public static class Reader
{
    public static string str1;
    public static string str2;
    public static string str3;

    private static void ReadXml(string s1, string s2, string s3)
    {
        XmlDocument doc = new XmlDocument();
        doc.Load(strXmlPath);
        str1 = s1;
        str2 = s2;
        str3 = s3;
    }
}

class xyz
{        
    string GetValue()
    {
        return Reader.str1;
    }
}

, . ? , .

, , ?

+3
3

, , , web.config ConfigurationManager . ( .)

:

<appSettings>
<add key="nameKey1" value="Value1"/>
<add key="nameKey2" value="Value2"/>
<add key="nameKey3" value="Value3"/>
</appSettings>

:

string key1 = System.Configuration.ConfigurationManager.AppSettings["nameKey1"];

,

( )

+2

, , - , , .

- , . - - - ( , ..) XML. , .

:

public interface ISettingReader
{
    int ReadSomeAppInt ();

    // TODO add other read functions
}

:

public class XmlAppSettingReader : ISettingReader
{
    public XmlAppSettingReader ( string sFilename )
    {
        // TODO open XML file and get ready to read settings
    }

    public int ReadSomeAppInt ()
    {
        // TODO read the named integer from XML and return it
    }
}

..

- (, GlobalSettings ..), , . Init .

public static class GlobalSettings
{
    // TODO add public static get properties for various settings
    private static int s_nSomeAppInt;

    public static void Init ( ISettingReader oReader )
    {
        // TODO use reader to read individual setting values

        int nSomeAppInt = oReader.ReadSomeAppInt ();

        // TODO validate nSomeAppInt and store it in private
        // static field
        s_nSomeAppInt = nSomeAppInt;
    }

    public static int SomeAppInt
    {
        get
        {
            return ( s_nSomeAppInt );
        }
    }
}

Main:

{
    ISettingReader oReader = new XmlAppSettingReader ( "C:\file.xml" );
    GlobalSettings.Init ( oReader );

    // TODO continue startup
}

. .

( ) GlobalSettings IGlobalSettings. IGlobalSettings , .

0

If your xml is in serialized format, like below:

<?xml version="1.0" encoding="utf-8"?>
<ArrayOfValues xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
    <Values>
        <Tag1>Value1</Tag1>
        <Tag2>Value2</Tag2>
        <Tag3>Value3</Tag3>
    </Values>
</ArrayOfValues>

And you have a class in which you want to deserialize, as shown below:

public class Values
{
    // set the types accordingly
    public string Tag1 { get; set; }
    public string Tag2 { get; set; }
    public string Tag3 { get; set; }
}

In addition, you may have a class containing deserialized code inside:

using System.Xml.Serialization;

namespace Serialization
{
    public class ValuesGenerator
    {
        private const string XmlPath = "SPECIFY THE XML FULL PATH HERE";

        public List<Values> GetColumnInfo()
        {
            var serializer = new XmlSerializer(typeof(List<Values>));
            return (List<Values>)serializer.Deserialize(new StreamReader(XmlPath));
        }
    }
}

You can then deserialize it at the beginning of your program, as shown below:

static class Program
{
    /// <summary>
    /// The main entry point for the application.
    /// </summary>
    [STAThread]
    static void Main()
    {
        Application.EnableVisualStyles();
        Application.SetCompatibleTextRenderingDefault(false);

        // look for the following 2 lines
        var valuesGenerator = new ValuesGenerator();
        List<Values> values = valuesGenerator.GetColumnInfo(); // This is the list which contains the deserialized info

        Application.Run(new class1());
    }
}
0
source

All Articles