Home > Blockchain >  How can I grab extract custom XML from web.config for use in another class?
How can I grab extract custom XML from web.config for use in another class?

Time:10-09

I currently have an xml file named PrioritizationSettings.config and I need to consolidate this into Web.config.

I have moved this directly into the Web.config as it is the same across all configurations.

I noticed that the project is using this old file path that no longer exists because I moved the XML directly into Web.config.

public static PrioritizationSettings LoadPrioritizeSettings()
        {
            return LoadPrioritizeSettings(AppDomain.CurrentDomain.BaseDirectory   "__Configs\\PrioritizationSettings.config");
        }

I would like to be able to access the PrioritizationSettings inside of Web.config from here. So that instead of passing the entire XML file, I can just pass in the section of XML that now exists in Web.Config

Is there another way to do this without using ConfigurationManager.GetSection()? I have looked at this an I fear it may be far more involving. I just need to extract the XML.

CodePudding user response:

This appears to be doing what I would like.

public static PrioritizationSettings LoadPrioritizeSettings()
        {
            return LoadPrioritizeSettings(AppDomain.CurrentDomain.BaseDirectory   "Web.config");
        }

I now pass the entire Web.config file in. Inside of the LoadPrioritizeSettings I have the following code:

public static PrioritizationSettings LoadPrioritizeSettings(string configFile)
    {
        XmlReader xmlReader;
        try { xmlReader = XmlReader.Create(configFile); }
        catch (Exception ex) { throw ex; }

        if (xmlReader == null)
            return null;

        XmlDocument xmlDoc = new System.Xml.XmlDocument();
        xmlReader.Read();
        xmlDoc.Load(xmlReader);
        xmlReader.Close();

        XmlNode xmlConfiguration = xmlDoc["configuration"];
        if (xmlConfiguration == null)
            throw new Exception("The root element (PrioritizationSettings) of the config file could not be found.");
        XmlNode xmlPrioritizeSettings = xmlConfiguration["PrioritizationSettings"];
        return prioritizeSettings;
    }

So I am able to get the PrioritizationSetting node from the web.config.

  • Related