comments are getting deleted while updating the we

2020-04-25 09:49发布

问题:

Comments and some keys are getting deleted while updating the web.config problematically

string path = ConfigurationManager.AppSettings["path"];
var map = new ExeConfigurationFileMap { ExeConfigFilename = path };
var configFile = ConfigurationManager.OpenMappedExeConfiguration(map, ConfigurationUserLevel.None);
configFile.AppSettings.Settings["StrKeyName"].Value = Convert.ToString(IntValue);
configFile.Save(); 

May I know that Why its happening like that, I want to retain those comments .please let me know How can I achieve it.

回答1:

There is not way to maintain the comments. It is how .NET implemented it. But you can have your own library that helps you to manipulate the configuration file using XmlDocument class.

I have created a similar class. Here is the class code in VB.NET.

Imports System.Xml
Imports System.Configuration
Public Class XConfiguration
    Private ConfigurationFilePath As String
    Private XmlDoc As XmlDocument
    Public Sub New()
        ConfigurationFilePath = Web.HttpContext.Current.Server.MapPath("/") & "web.config"
        XmlDoc = New XmlDocument() With {.PreserveWhitespace = True}
        Try
            XmlDoc.Load(ConfigurationFilePath)
        Catch e As System.IO.FileNotFoundException
            Throw New Exception("No configuration file found.", e)
        End Try
    End Sub
    Public Sub WriteConnectionString(ByVal Name As String, ByVal ConnectionString As String,Optional ByVal SaveImmediately As Boolean = False)
        Dim NodeConnectionStrings As XmlNode = XmlDoc.SelectSingleNode("//connectionStrings")
        If NodeConnectionStrings Is Nothing Then Throw New InvalidOperationException("connectionStrings section not found in config file.")

        Dim ElemAdd As XmlElement = CType(NodeConnectionStrings.SelectSingleNode(String.Format("//add[@name='{0}']", Name)), XmlElement)

        If ElemAdd IsNot Nothing Then
            ElemAdd.SetAttribute("connectionString", ConnectionString)
        Else
            ElemAdd = XmlDoc.CreateElement("add")
            ElemAdd.SetAttribute("name", Name)
            ElemAdd.SetAttribute("connectionString", ConnectionString)
            NodeConnectionStrings.AppendChild(ElemAdd)
        End If

        If SaveImmediately Then Save()
    End Sub
    Public Function ReadSetting(ByVal Key As String) As String
        Return ConfigurationManager.AppSettings(Key)
    End Function
    Public Sub WriteAppSetting(ByVal Key As String, ByVal Value As String, Optional ByVal SaveImmediately As Boolean = False)
        Dim NodeAppSettings As XmlNode = XmlDoc.SelectSingleNode("//appSettings")
        If NodeAppSettings Is Nothing Then Throw New InvalidOperationException("appSettings section not found in config file.")

        Dim ElemAdd As XmlElement = CType(NodeAppSettings.SelectSingleNode(String.Format("//add[@key='{0}']", Key)), XmlElement)

        If ElemAdd IsNot Nothing Then
            ElemAdd.SetAttribute("value", Value)
        Else
            ElemAdd = XmlDoc.CreateElement("add")
            ElemAdd.SetAttribute("key", Key)
            ElemAdd.SetAttribute("value", Value)
            NodeAppSettings.AppendChild(ElemAdd)
        End If

        If SaveImmediately Then Save()
    End Sub
    Public Sub RemoveSetting(ByVal Key As String, Optional ByVal SaveImmediately As Boolean = False)
        Dim NodeAppSettings As XmlNode = XmlDoc.SelectSingleNode("//appSettings")

        If NodeAppSettings Is Nothing Then
            Throw New InvalidOperationException("appSettings section not found in config file.")
        Else
            NodeAppSettings.RemoveChild(NodeAppSettings.SelectSingleNode(String.Format("//add[@key='{0}']", Key)))
        End If

        If SaveImmediately Then Save()
    End Sub
    Public Sub Save()
        XmlDoc.Save(ConfigurationFilePath)
    End Sub
End Class