开发者

Is there a standard way to store arbitary settings in an XML file for use by .net?

Ideally I'd be using files of the same basic structure as an app.config and pulling settings buy so long as I can get any sort of "pathed" XML storage for set开发者_运维百科tings I'd be okay.

Ideally given something like :

<Root>
 <ApplicationSettings>
  <Setting key="SomeKey", value="SomeValue"/>
 </ApplicationSettings>
</Root>

I'd call something like:

Settings.Get("ApplicationSettings.SomeKey");

and get "SomeValue" as an lresult.

Anyone know of any easy standard commonly in place way to do this?


I'm not sure if I understand the question, as you seem to be familiar with the App.Config file... There is a method very similar to what you posted. Create an App.Config file for your project, and put the following code in it:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <appSettings>
        <add key="Key1" value="Value1" />
        <add key="Key2" value="Value2" />
    </appSettings>
</configuration>

And then you can access it in your code using:

string key1= ConfigurationSettings.AppSettings["Key1"];


Yes.

A long time ago, Craig Andera has described the last configuration handler you'll ever need. It uses app.config to provide the configuration. You could modify it to get the config from somewhere else.

EDIT
It appears as though the above link has been moved
http://sites.google.com/site/craigandera/craigs-stuff/clr-workings/the-last-configuration-section-handler-i-ll-ever-need


The standard is to use app.config/web.config.

I have no idea where you heard that changing it is not best practice - I think someone is trying to fool you.


You can build a function that pulls the Value. I have done this

 Public Class Settings
     Public Function GetSetting(Byval SettingsKey As String) As String

        Dim xDoc As New XmlDocument
        xDoc.Load("app.config")
        Dim xNode As XmlNode
        xNode = xDoc.SelectSingleNode(("Settings/setting[@item='" & SettingsKey & "']"))
        If xNode Is Nothing Then
            Return String.Empty
        Else
            Return xNode.Attributes("value").Value
        End If
        xDoc = Nothing

    End Sub
End Class

And then call it like this

Settings.GetSetting("MyArbitraryKey")
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜