Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Chriztian Steinmeier 2800 posts 8791 karma points MVP 8x admin c-trib
    Mar 30, 2015 @ 10:22
    Chriztian Steinmeier
    0

    How to access a setting from a custom .config (XML) file?

    Hi all,

    I have a SiteSettings.config file in the Config/ folder that looks something like this:

    <SiteSettings siteName="TheGames">
        <DeploymentKeys>
            <headlessWarrior>Ned Stark</headlessWarrior>
        </DeploymentKeys>
        <!-- etc. -->
    </SiteSettings>
    

    I'd like to read the value of the headlessWarrior element from a Partial View - what's the correct way to do that?

    (I can read it pretty easily from an XSLT macro with the document() function but I have no idea how to go about grabbing it from a Razor view.)

    /Chriztian

  • Alex Skrypnyk 6175 posts 24176 karma points MVP 8x admin c-trib
    Mar 30, 2015 @ 12:06
    Alex Skrypnyk
    1

    Hi Chriztian,

    You can read file as xml doc and parse it like that :

    System.Xml.Linq.XDocument doc = System.Xml.Linq.XDocument.Parse(strValue);
    
    
    var solutiuonValue = (
    
                from f in doc.Elements().FirstOrDefault().Descendants().Where(x => x.Name.Equals("headlessWarrior"))
    
                select f).FirstOrDefault();
    

    Thanks, Alex

  • Chriztian Steinmeier 2800 posts 8791 karma points MVP 8x admin c-trib
    Mar 30, 2015 @ 12:15
    Chriztian Steinmeier
    0

    Hi Alex,

    Thanks - I did think about something like that, though I'd much prefer XPath to get the value :)

    The Parse() method won't work with a URL, I guess? Looks like there's a Load() method I can use - if it allows loading a .config file from the Config/ folder...

    /Chriztian

  • Justin 19 posts 163 karma points
    Apr 20, 2015 @ 00:50
    Justin
    0

    Hi Alex,

    Can't you use standard asp.net configuratioManager to retrieve appSettings?

    var warrior = System.Configuration.ConfigurationManager.AppSettings["headlessWarrior"];

    You will need to add a reference to System.Configuration to get to this extension.

  • Chriztian Steinmeier 2800 posts 8791 karma points MVP 8x admin c-trib
    Apr 20, 2015 @ 07:57
    Chriztian Steinmeier
    0

    Hi Justin,

    The settings are in a custom .config file (custom XML format too) so I don't think the ConfigurationManager would know how to access those settings, unless it has some configuration settings of its own that could teach it how to parse that file...?

    /Chriztian

Please Sign in or register to post replies

Write your reply to:

Draft