以编程方式更新Web配置文件时,注释将被删除

时间:2016-02-25 09:29:33

标签: c# asp.net c#-4.0

有问题地更新web.config时,注释和某些键被删除

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(); 

我可以知道为什么会这样发生,我想保留这些评论。请告诉我如何实现它。

1 个答案:

答案 0 :(得分:1)

没有办法维护评论。 .NET就是这样实现的。 但是您可以拥有自己的库,该库可以帮助您使用XmlDocument类来操纵配置文件。

我创建了一个类似的类。这是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