As part of my installation process, I update some values in my app.config files,
The file is built and indented in a way that helps the user read the values.
It’s built in sections, each section is an XElement and each value is an XAttribute.
This is a sample for the sections:
<DataAccessSection
DataMappingFile = "Data\Mapping.xml"
SchemaKeyWord = "[generated]"
/>
<ServiceManagementSection
ServerName = "dummy"
ServerIP = "1.0.0.0"
Port = "1"
/>
I edit the file using an XDocument, accessing the XElement of the section and changing the XAttribute’s value, and then saving the changes using xDocument.Save(fullpath);
After the file is saved, the structure of the config file is this:
<DataAccessSection DataMappingFile="Data\Mapping.xml" SchemaKeyWord="[generated]" />
<ServiceManagementSection ServerName="dummy" ServerIP="1.0.0.0" Port="1" />
This structure is not so clear to the users, how do I keep the file in the initial structure and change only the values and not the indentation?
I could open and edit it as a simple text file, but it seems silly to access it like a text file when I have the power of a structured XML file.
EDIT:
Thanks for your answers.
I tried the solutions in the answers, but it keeps removing my white spaces and the indentation.
I also tried to load using:
XDocument.Load(fullPath, LoadOptions.PreserveWhiteSpaces);
But it doesn’t fix my problem.
The only thing that keeps all the whitespaces is loading it as a text file, and editing using String manipulation, but I don’t want to to that.
Does anyone has a different approach that will fix this problem?
Maybe using a different package for XML editing?
Use LoadOptions.PreserveWhitespace when you calling to xDocument.Load and SaveOptions.DisableFormatting when you Saving