Link to home
Start Free TrialLog in
Avatar of mtsnh
mtsnh

asked on

Why am I getting xmlNode,xmlElement, and AppSettings is not defined?

I found this VB.Net code on another question and tried to implement it in my program.  I have narrowed down to 3 errors that have me baffled.  I am using VS 2003 in an XP Pro environment.  

errors:
Performing main compilation...
...: error BC30002: Type 'AppSettings' is not defined.
... : error BC30002: Type 'XmlNode' is not defined.
... : error BC30002: Type 'XmlElement' is not defined.
Building satellite assemblies...      

Code:
 Dim strAppPath, strAppName As String
        strAppName = Mid(Application.ExecutablePath, InStrRev(Application.ExecutablePath, "\") + 1)
        MsgBox(strAppName)
        strAppPath = Mid(Application.ExecutablePath, 1, InStrRev(Application.ExecutablePath, "\") - 1)
        MsgBox(strAppPath)
        Dim xmlNode As String

        Dim s As String = ""
        s = Me.getConfigValue("v4")
        If s Is Nothing Then
            Console.WriteLine( _
               "Adding new configuration value.")

            ' Get the configuration file name
            Dim configFile As String = strAppName + ".config"

            ' create a new XmlDocument object
            Dim Xml As XMLDocument = New XMLDocument

            ' load the configuration file
            Xml.Load(configFile)

            ' get a reference to the <appSettings> section
' *********Here is the first problem

            Dim appSettingsElement As _
              XmlNode = Xml.SelectSingleNode( _
               "//configuration/appSettings")
'***********
            ' create a new <add> element

' *********Here is the second problem

            Dim newElement As XmlElement = _
               Xml.createElement("add")
'*************
            ' set its attributes
            newElement.SetAttribute("key", "v4")
            newElement.SetAttribute( _
               "value", "Value 4")

            ' append it to the <appSettings> section
            appSettingsElement.AppendChild(newElement)
            Xml.Save(configFile)

            ' changes are not live until
            ' the app is restarted--the following
            ' line prints nothing
            Console.WriteLine(Me.getConfigValue("v4"))
        Else
            Console.WriteLine("Configuration value " & _
               """v4"" already exists." & _
               "  Value: " & s)
        End If
    End Sub
Avatar of surajguptha
surajguptha
Flag of United States of America image

I am not sure about these errors but if you are looking to add app setting keys programatically. here is a way to do it
http://msmvps.com/blogs/simpleman/archive/2005/06/23/54733.aspx

Also it may have occurred because of the escape characters. try "//configuration//appSettings"
I used this code and it worked fine...

Dim strAppPath, strAppName As String
        strAppName = Mid(Application.ExecutablePath, InStrRev(Application.ExecutablePath, "\") + 1)
        MsgBox(strAppName)
        strAppPath = Mid(Application.ExecutablePath, 1, InStrRev(Application.ExecutablePath, "\") - 1)
        MsgBox(strAppPath)
        Dim xmlNode As String

        Dim s As String
        's = Me.getConfigValue("v4")

        If s Is Nothing Then
            Console.WriteLine( _
               "Adding new configuration value.")

            ' Get the configuration file name
            Dim configFile As String = strAppName + ".config"

            ' create a new XmlDocument object
            Dim Xml As Xml.XmlDocument = New Xml.XmlDocument

            ' load the configuration file
            Xml.Load(configFile)

            ' get a reference to the <appSettings> section
            ' *********Here is the first problem

            Dim appSettingsElement As _
              Xml.XmlNode = Xml.SelectSingleNode( _
               "//configuration/appSettings")
            '***********
            ' create a new <add> element

            ' *********Here is the second problem

            Dim newElement As Xml.XmlElement = _
               Xml.createElement("add")
            '*************
            ' set its attributes
            newElement.SetAttribute("key", "v4")
            newElement.SetAttribute( _
               "value", "Value 4")

            ' append it to the <appSettings> section
            appSettingsElement.AppendChild(newElement)
            Xml.Save(configFile)

            ' changes are not live until
            ' the app is restarted--the following
            ' line prints nothing
            'Console.WriteLine(Me.getConfigValue("v4"))
        Else
            Console.WriteLine("Configuration value " & _
               """v4"" already exists." & _
               "  Value: " & s)
        End If
Avatar of mtsnh
mtsnh

ASKER

Thanks for your help.  I believe we are on to something.  When I place this in my code the problem seems to go away, however depending on where I put it, it causes more problems.  

Public Interface ConfigurationManager
    Inherits IDispatch
What is this doing and where should it be placed.  I found it in here:

http://msmvps.com/blogs/simpleman/archive/2005/06/23/54733.aspx
Well isnt this link as the link i made available in my first post. Confused :)

What problems are you facing doing this? Because i was able to successfully able to add the key into my config file dynamically
Avatar of mtsnh

ASKER

It is like a reference is missing or the interface and inherits from these lines.

Public Interface ConfigurationManager
    Inherits IDispatch

If I add these to lines to the top of my code, my original problem goes away however I now get many more errors with the rest of my code and I think this is the cause:  

.... Interface members must be methods, properties, events, or type definitions.

I have never done anything with interface members so I am slightly confused to what is going on.

Thanks again,

Kevin
I think the link talks about how to add the appsetting keys using .net 2.0, since you are using .net 1.1 it might not work. I didnt factor the .net 1.1 limitation because u mentionned that u are using VS2003 but again since it was posted in .net framework 2.0 i posted this link.

The code that i had pasted works fine. I have tested it and it ended up adding the app setting key. I paste the code again here. Please use this and tweek it, it should work just fine.

Dim strAppPath, strAppName As String
        strAppName = Mid(Application.ExecutablePath, InStrRev(Application.ExecutablePath, "\") + 1)
        MsgBox(strAppName)
        strAppPath = Mid(Application.ExecutablePath, 1, InStrRev(Application.ExecutablePath, "\") - 1)
        MsgBox(strAppPath)
        Dim xmlNode As String

        Dim s As String
        's = Me.getConfigValue("v4")

        If s Is Nothing Then
            Console.WriteLine( _
               "Adding new configuration value.")

            ' Get the configuration file name
            Dim configFile As String = strAppName + ".config"

            ' create a new XmlDocument object
            Dim Xml As Xml.XmlDocument = New Xml.XmlDocument

            ' load the configuration file
            Xml.Load(configFile)

            ' get a reference to the <appSettings> section
            ' *********Here is the first problem

            Dim appSettingsElement As _
              Xml.XmlNode = Xml.SelectSingleNode( _
               "//configuration/appSettings")
            '***********
            ' create a new <add> element

            ' *********Here is the second problem

            Dim newElement As Xml.XmlElement = _
               Xml.createElement("add")
            '*************
            ' set its attributes
            newElement.SetAttribute("key", "v4")
            newElement.SetAttribute( _
               "value", "Value 4")

            ' append it to the <appSettings> section
            appSettingsElement.AppendChild(newElement)
            Xml.Save(configFile)

            ' changes are not live until
            ' the app is restarted--the following
            ' line prints nothing
            'Console.WriteLine(Me.getConfigValue("v4"))
        Else
            Console.WriteLine("Configuration value " & _
               """v4"" already exists." & _
               "  Value: " & s)
        End If
Avatar of mtsnh

ASKER

I finally figured it out!!!!!  Yippee.   I was missing one stupid line of code!

Imports System.Xml

Thanks for your help.
argghhhh !!!! I was looking at the code assuming the imports were already in place...
Avatar of mtsnh

ASKER

There is still something that I am not doing correct.  That code works fine but I don't think it is doing what I planned.  It seems to be overwriting the config file everytime and I can't figure out how to read a value.  There are to many assumptions made in all the examples I have studied.  I still have something missing in order to read a value in a config file.  When I try the code below, I still get type appsettings is not defined which is either a missing reference or import.  Any new ideas would be helpful.

        Dim a As New AppSettings(AppSettings.Config.PrivateFile)
        Dim myString As String = a.GetSetting("SettingName")
Try this
Dim myString as String = ConfigurationSettings.AppSettings("SettingName")
Avatar of mtsnh

ASKER

Thanks!  That got rid of the error but I am still not getting a value.  Is that file being replaced everytime it is being run?  What is causing that to happen if it is?  We are very close to solving this mystery!
There will be two app config files. One in the project called app.config and another one by the name Applicationname.exe.config

Which one has the changed value after u run your program??
Avatar of mtsnh

ASKER

I was under the impression that this code only deals with the Applicationname.exe.config file.  Haven't really paid any attention to the other one.  That appears to have been the same for a very long time.
ASKER CERTIFIED SOLUTION
Avatar of surajguptha
surajguptha
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of mtsnh

ASKER

Thank you ! Thank you ! Thank you !  I was wondering where all the information that kept getting written to the Applicationname.exe.config file was coming from.  I didn't see it anywhere in the code and it was a mystery until now.  I never really got a good explaination of the app.config file.  Now I know that is where I want to make my changes that I want to distribute.  It is all coming together for me now.

Thanks very much!
you are welcome. Best of luck :)