实际更改web.config安全模式(编辑XML文件)

本文关键字:编辑 XML 文件 安全模式 config web | 更新日期: 2023-09-27 18:28:29

如果我们有这样的配置。。。

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <system.serviceModel>
        <services>
    </services>
    <bindings>
      <basicHttpBinding>
        <binding name="serviceConfiguration" closeTimeout="00:01:00" openTimeout="00:01:00" receiveTimeout="00:01:00" sendTimeout="00:01:00" maxReceivedMessageSize="33554432" messageEncoding="Text" textEncoding="utf-8">
            <readerQuotas maxDepth="32" maxStringContentLength="524288" maxArrayLength="1048576"
                maxBytesPerRead="4096" maxNameTableCharCount="16384" />
          <security mode="TransportCredentialOnly">
            <transport clientCredentialType="Windows" />
          </security>
        </binding>
      </basicHttpBinding>
    </bindings>
    </system.serviceModel>
</configuration>

我想换

<security mode="TransportCredentialOnly"><security mode="Transport">

<transport clientCredentialType="Windows" /><transport clientCredentialType="None" />

到目前为止,我已经读取了xml文件和安全节点

        WebConfig = @"c:'xml.xml";
        XmlDocument myXmlDocument = new XmlDocument();
        myXmlDocument.Load(WebConfig);
        XmlNodeList oldNodes;
        oldNodes = myXmlDocument.GetElementsByTagName("security");

但我不确定如何更改XML节点并将其保存回文件中。

我们需要这样做,因为有时我们必须在部署后手动更改配置,而且有数百个配置,所以我实际地递归地遍历文件并结束它们。

实际更改web.config安全模式(编辑XML文件)

如果您想继续使用XmlDocument,可以按照以下示例按名称和特定属性值选择元素:

....
//select <security> element having mode attribute value equals "TransportCredentialOnly"
XmlNode security = myXmlDocument.SelectSingleNode("//security[@mode='TransportCredentialOnly']");
if(security != null)
{
    //edit the attribute value
    security.Attributes["mode"].Value = "Transport";
}
....
//save edited XmlDocument back to file    
myXmlDocument.Save(WebConfig);