Hi hannous, I am still not able to succeed and kindly help me to succeed with this problem. Here is my code and xml file.
Imports
System.XmlModule Module1
Dim FileLocation As String = "C:\webroot\web.config"
Dim NodeLocation As String = "configuration/appSettings"Sub Main()
Dim x As Boolean = AddKey(FileLocation, NodeLocation)
If (x) ThenConsole.WriteLine("success")
ElseConsole.WriteLine("failed")
End If'Adds a key and value to the configuration file
End Sub
Public Function AddKey(ByVal strKey As String, ByVal strValue As String) As BooleanDim xmlDoc As New XmlDocument
'Change this to the location of your configuration file
xmlDoc.Load(FileLocation)
Dim XmlNamespaceManager As System.Xml.XmlNamespaceManager = New System.Xml.XmlNamespaceManager(xmlDoc.NameTable)
XmlNamespaceManager.AddNamespace(
"ns", "http://schemas.microsoft.com/.NetConfiguration/v2.0")
'Change this if node is differentDim appSettingsNode As XmlNode = xmlDoc.SelectSingleNode(NodeLocation, XmlNamespaceManager)
Try
'Check if the node exists before adding it
If (KeyExists(strKey)) ThenThrow New ArgumentException("Key name: <" + strKey + "> already exists in the configuration.")
End If
'You must have at least one key that can be emptyDim newChild As XmlNode = appSettingsNode.FirstChild.Clone
newChild.Attributes(
"key").Value = strKeynewChild.Attributes("value").Value = strValue
appSettingsNode.AppendChild(newChild)
xmlDoc.Save(FileLocation)
Return TrueCatch ex As Exception
Return False
End Try
End Function
'Determines if a key exists
Public Function KeyExists(ByVal strKey As String) As BooleanDim xmlDoc As New XmlDocument
xmlDoc.Load(FileLocation)
Dim appSettingsNode As XmlNode = xmlDoc.SelectSingleNode(NodeLocation)
Dim childNode As XmlNodeFor Each childNode In appSettingsNode
If (childNode.Attributes("key").Value = strKey) Then
Return True
End If
Next
Return FalseEnd Function
End
Module
Here is the XML file code:
<?xml version="1.0"?>
<configuration xmlns="http://schemas.microsoft.com/.NetConfiguration/v2.0">
<appSettings>
<!-- point to the database you want to use-->
<add key="ConnStr" value="Server=servername; database=databasename;Integrated Security=SSPI;Connect Timeout=0" />
<!-- specify the full path of the rolespace.xml-->
<add key="ROLESPACE" value="E:\webroot\OAWebService\oaweb.xml" />
<add key="TOCLevelDepth" value="3" />
<add key="SearchMax" value="500" />
</appSettings>
</configuration>
-----------
I am receiving nothing as the value for appsettingsNode variable. Kindly, let me know where i'm going wrong and help me out.