【问题标题】:comments are getting deleted while updating the web config file programmatically以编程方式更新 Web 配置文件时,评论被删除
【发布时间】:2016-02-25 09:29:32
【问题描述】:

在更新 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(); 

我可以知道为什么会这样,我想保留那些 cmets。请让我知道如何实现它。

【问题讨论】:

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


【解决方案1】:

没有办法维护 cmets。这就是 .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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-09-23
    • 2011-10-11
    • 1970-01-01
    相关资源
    最近更新 更多