Louis Rhys
Louis Rhys

Reputation: 35627

Is there a helper method to parse an appSettings-like xml?

By appSettings-like I mean like this,

<appSettings>
    <add key="myKey" value="myValue" />
</appsettings>

The result is a key-value collection that I can access like:

string v = config["myKey"];

but it is not necessarily located in app.config, so what I have is a string or a XmlNode.

NameValueFileSectionHandler.Create method apparently can do the job, but the input needs two objects, Object parent, Object configContext, in addition to a xml node, and I don't know what to pass to them.

Upvotes: 5

Views: 2916

Answers (4)

Andrej Slivko
Andrej Slivko

Reputation: 1256

Parse a string to a dictionary like this,

var xml = XElement.Parse("<appSettings><add key=\"myKey\" value=\"myValue\" /></appSettings>");
var dic = xml.Descendants("add").ToDictionary(x => x.Attribute("key").Value, x => x.Attribute("value").Value);

You can get the values like this,

var item = dic["myKey"];

You can also modify the values in the dictionary like this,

dic["myKey"] = "new val";

And you can convert the modified dictionary back to a XElement using this code,

var newXml = new XElement("appSettings", dic.Select(d => new XElement("add", new XAttribute("key", d.Key), new XAttribute("value", d.Value))));

Upvotes: 7

Dave Long
Dave Long

Reputation: 794

You could do something like this :

Hashtable htResource = new Hashtable();
    XmlDocument document = new XmlDocument();
    document.LoadXml(XmlString);
    foreach (XmlNode node in document.SelectSingleNode("appSettings"))
    {
        if ((node.NodeType != XmlNodeType.Comment) && !htResource.Contains(node.Attributes["name"].Value))
            {
                htResource[node.Attributes["name"].Value] = node.Attributes["value"].Value;
            }
    }

Then you can access the values using:

string myValue = htResource["SettingName"].ToString();

Hope that helps,

Dave

Upvotes: 1

IndigoDelta
IndigoDelta

Reputation: 1481

I haven't tried this but I think the code below might give you what you want.

System.Configuration.ConfigurationFileMap fileMap = new System.Configuration.ConfigurationFileMap(yourConfigFileLocation);
System.Configuration.Configuration configuration = System.Configuration.ConfigurationManager.OpenMappedMachineConfiguration(fileMap);
        var setting = configuration.AppSettings["settingName"];

Upvotes: -1

Adam Houldsworth
Adam Houldsworth

Reputation: 64487

It isn't really a "helper", but the framework includes something branded the Configuration API. This works inside configuration files and will translate your configuration XML into classes. Take a look in more detail here:

http://msdn.microsoft.com/en-us/library/ms178688(v=VS.80).aspx http://msdn.microsoft.com/en-us/library/2tw134k3.aspx

This is always the sample I use (because I can never remember the exact structure of it all):

http://haacked.com/archive/2007/03/12/custom-configuration-sections-in-3-easy-steps.aspx

http://www.codeproject.com/KB/dotnet/mysteriesofconfiguration.aspx#

There is also a designer available that integrates with VS:

http://csd.codeplex.com/

Googling "C# custom configuration sections" will also yield a lot of results for you.

Upvotes: 0

Related Questions