0

If we have a config that looks like this...

<?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>

I would like to change

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

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

So far i have read the xml file and read the security node

        WebConfig = @"c:\xml.xml";

        XmlDocument myXmlDocument = new XmlDocument();
        myXmlDocument.Load(WebConfig);

        XmlNodeList oldNodes;
        oldNodes = myXmlDocument.GetElementsByTagName("security");

But i'm unsure how to change the XML nodes and save it back to the file.

We need to do this as sometimes we have to change configs manually after deployment and there are hundreds of them, so i'm pragmatically recursively going through the files and ending them.

MrBeanzy
  • 2,286
  • 3
  • 28
  • 38
  • 1
    You may want to look into using the [ConfigurationManager](http://msdn.microsoft.com/en-us/library/system.configuration.configurationmanager(v=vs.110).aspx) class. It's designed for working with config files, rather than rolling your own more generic xml code. You should find strongly typed classes representing each section of the file that you can directly cast things to, and work with properties etc instead of searching for things by name. – James Thorpe Oct 30 '14 at 10:05
  • Alternatively, if you want to stick with `XmlDocument` - [this question](http://stackoverflow.com/questions/2558787/how-to-modify-exiting-xml-file-with-xmldocument-and-xmlnode-in-c-sharp?rq=1) has an answer with the correct way of doing things. – James Thorpe Oct 30 '14 at 10:09

1 Answers1

0

In case you want to continue using XmlDocument, you can follow this example to select an element by name and specific attribute value :

....

//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);
har07
  • 88,338
  • 12
  • 84
  • 137