以编程方式更新Web配置文件时,注释将被删除 [英] comments are getting deleted while updating the web config file programmatically

查看:89
本文介绍了以编程方式更新Web配置文件时,注释将被删除的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

推荐答案

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

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.

我创建了一个类似的类。这是VB.NET中的类代码。

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

这篇关于以编程方式更新Web配置文件时,注释将被删除的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆