Results 1 to 6 of 6

Thread: Adding a new entry in the config in code

  1. #1

    Thread Starter
    Fanatic Member venerable bede's Avatar
    Join Date
    Sep 2002
    Location
    The mystic land of Geordies
    Posts
    1,018

    Adding a new entry in the config in code

    I have the following code which takes in a string representing an element in my settings/config file. It then returns the value of that element.

    Code:
    public string GetAppConfigValue(string SettingName)
    {
                string sContents;
                ConfigurationSectionGroup grpApp = config.GetSectionGroup("applicationSettings");
                ClientSettingsSection secPropSet = (ClientSettingsSection)grpApp.Sections[sSettingsSection];
                secPropSet.SectionInformation.ForceSave = true;
                SettingElement elm = secPropSet.Settings.Get(SettingName);
                if (elm == null)
                {
                    sContents = SettingName + " is missing!";
                }
                else
                {
                    sContents = elm.Value.ValueXml.InnerText.Trim();
                }
                return sContents;
    }
    This all works fine but what I need to do is create this setting with a string "NOT AVAILABLE"if it is not there.

    I think its something along the lines off :
    Code:
    SettingElement newElem = new SettingElement(SettingName, SettingsSerializeAs.String);
    secPropSet.Settings.Add(newElem);
    //How do i add "NOT AVAILABLE" to its value
    The above code adds this to the config "<setting name="NEW_ATTRIBUTE" serializeAs="String" />" but it has no value and no closing tag " </setting>"
    Man am I ever confused :-(
    Last edited by venerable bede; Aug 5th, 2008 at 11:16 AM.

    Parksie

  2. #2
    type Woss is new Grumpy; wossname's Avatar
    Join Date
    Aug 2002
    Location
    #!/bin/bash
    Posts
    5,682

    Re: Adding a new entry in the config in code

    SettingElement has a Value property, you need to populate that appropriately before the Add() call. MSDN has lots of info about it.

  3. #3

    Thread Starter
    Fanatic Member venerable bede's Avatar
    Join Date
    Sep 2002
    Location
    The mystic land of Geordies
    Posts
    1,018

    Re: Adding a new entry in the config in code

    Thanks Wossy.

    I'm still having tons of trouble with this one though. I have the following :
    Code:
    SettingElement newElem = new SettingElement(SettingName, SettingsSerializeAs.String);
    SettingValueElement setValue;
    System.Xml.XmlNode xNode = new System.Xml.XmlNode();
    xNode.Value = "TESTING";
    setValue.ValueXml = xNode;
    newElem.Value = setValue;
    secPropSet.Settings.Add(newElem);
    All i get is :

    Cannot create an instance of the abstract class or interface 'System.Xml.XmlNode'

    However the value of newElem requires something of XMLNode type.

    Parksie

  4. #4

    Thread Starter
    Fanatic Member venerable bede's Avatar
    Join Date
    Sep 2002
    Location
    The mystic land of Geordies
    Posts
    1,018

    Re: Adding a new entry in the config in code

    I have managed to get a bit further :
    Code:
    SettingElement newElem = new SettingElement(SettingName, SettingsSerializeAs.String);
    System.Xml.XmlDocument doc = new System.Xml.XmlDocument();
    System.Xml.XmlNode n = doc.CreateNode(System.Xml.XmlNodeType.Text, "bigWoof", "");
    SettingValueElement setValue = new SettingValueElement();
    setValue.ValueXml = n; 
    newElem.Value = setValue;
    secPropSet.Settings.Add(newElem);
    This code generates the following :
    Code:
     <setting name="HELPURL" serializeAs="String">bigWoof</setting>
    Note the lack of <value></value> tags.

    When I try and read the element I get the following :
    The configuration section cannot contain a CDATA or text element.

    Man this is doing my head in.

    Parksie

  5. #5
    C# Aficionado Lord_Rat's Avatar
    Join Date
    Sep 2001
    Location
    Cave
    Posts
    2,497

    Re: Adding a new entry in the config in code

    I use my own class so I must confess I'm not used to the built in save settings functions you are using, but my class works pretty well. Here are the two functions in their entirety:

    Code:
    //Written by Keith Ratliff - if you use, please leave this comment in your code.
    		static object oKey = new object();
    		public static void SaveSetting(string sSetting, string sValue)
    		{
    			lock(oKey){
    				string sFolder = System.Windows.Forms.Application.StartupPath;
    				System.Xml.XmlDocument oXD = new System.Xml.XmlDocument();
    				try{
    				oXD.Load(sFolder + "/settings.xml");
    				}catch(System.Exception)
    				{
    					string sNewSettings = @"<Root></Root>";
    					oXD.LoadXml(sNewSettings);
    				}
    				if(oXD.DocumentElement.SelectSingleNode(sSetting) != null)
    					oXD.DocumentElement.RemoveChild(oXD.DocumentElement.SelectSingleNode(sSetting));
    				System.Xml.XmlNode oNode = oXD.CreateNode(System.Xml.XmlNodeType.Element.ToString().ToLower(), sSetting, "");
    				oNode.InnerXml = sValue;
    				oXD.DocumentElement.AppendChild(oNode);
    				oXD.Save(sFolder + "/settings.xml");
    			}
    		}
    		
    		public static string GetSetting(string sSetting)
    		{
    			string sRet = "";
    			bool bOK = true;
    			lock(oKey){
    				string sFolder = System.Windows.Forms.Application.StartupPath;
    				System.Xml.XmlDocument oXD = new System.Xml.XmlDocument();
    				try{
    					oXD.Load(sFolder + "/settings.xml");
    				}catch(System.Exception){
    					sRet = "";
    					bOK = false;
    				}
    				
    				System.Xml.XmlNode oNode = null;
    				if(bOK){
    					oNode = oXD.DocumentElement.SelectSingleNode(sSetting);
    					if(oNode == null) {
    						sRet = "";
    						bOK = false;
    					}
    				}
    				
    				if(bOK){
    					sRet = oNode.InnerXml;
    				}
    			}
    			return sRet;
    		}
    If you want the error message you are describing (when the node doesn't already exist at all) just change
    if(oNode == null) {
    sRet = "";
    bOK = false;
    }

    to

    if(oNode == null) {
    sRet = "NOT AVAILABLE!";
    bOK = false;
    }

    EDIT: Adding thread safety.
    Last edited by Lord_Rat; Aug 13th, 2008 at 01:27 PM.
    Need to re-register ASP.NET?
    C:\WINNT\Microsoft.NET\Framework\v#VERSIONNUMBER#\aspnet_regiis -i

    (Edit #VERSIONNUMBER# as needed - do a DIR if you don't know)

  6. #6
    C# Aficionado Lord_Rat's Avatar
    Join Date
    Sep 2001
    Location
    Cave
    Posts
    2,497

    Re: Adding a new entry in the config in code

    Quote Originally Posted by venerable bede
    The above code adds this to the config "<setting name="NEW_ATTRIBUTE" serializeAs="String" />" but it has no value and no closing tag " </setting>"
    Man am I ever confused :-(
    Taking a second look at your original, I should probably point something out about XML.

    When a tag has no data inside, it is allowed to close itself by having a slash before the closing ">"

    For example:
    <SampleTag></SampleTag>

    is exactly the same as
    <SampleTag />

    Notice the "/>" in the second one? That means it closes itself.

    HTH
    Need to re-register ASP.NET?
    C:\WINNT\Microsoft.NET\Framework\v#VERSIONNUMBER#\aspnet_regiis -i

    (Edit #VERSIONNUMBER# as needed - do a DIR if you don't know)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width