1

I am using ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None); to open a configuration file, and then eventually calling Save() on that config to update the config. The saving portion is working fine, but the resulting format is different than what the input was.

Is there a way to keep the formatting the same when calling Configuration.Save()?

In the code below, I essentialy want the "After" block to look like the "Before" block after I call Save() and update the value I need.

Before

<items>
    <item domainName="DEV" url="http:/server1:8080" certificatePath="./cert1.cert" password="abc"/>
    <item domainName="QA" url="http://server2:8080" certificatePath="./cert2.cert" password="123"/>
    <item domainName="PROD" url="http://server3:8080" certificatePath="./cert3.cert" password="frt"/>
</items>

After

<items>
    <item domainName="DEV" url="http:/server1:8080"
        certificatePath="./cert1.cert" password="abc"/>
    <item domainName="QA" url="http://server2:8080"
        certificatePath="./cert2.cert" password="123"/>
    <item domainName="PROD" url="http://server3:8080"
        certificatePath="./cert3.cert" password="UpdatedProperty"/>
</items>
Sharpiro
  • 2,305
  • 2
  • 18
  • 27

1 Answers1

1

You can't control the formatting using the ConfigurationManager class. According to this StackOverflow question, the class simply does not look for things like whitespace or comments when it parses the document. Likewise, the class is sealed so you can't derive your own solution to the problem.

You could parse it with XDocument or XmlDocument to find the keys you need to update and save them while preserving the whitespace. Obviously not ideal but at the moment it seems like that is the only option.

Community
  • 1
  • 1
Justin K
  • 239
  • 2
  • 13