My goal is to output the modified XML file and preserve the special indentation that was present in the source file. The goal is to make the resulting file still look like the original, making it easier to compare and merge through the original control.
My program will read the XML file and add or modify one specific attribute.
Here is the formatting I'm trying to achieve / save:
<Base Import="..\commom\style.xml">
<Item Width="480"
Height="500"
VAlign="Center"
Style="level1header">
(...)
In this case, I just want to align all the attributes that have passed the first with the first.
XmlWriterSettings provides formatting options, but they will not achieve the result I'm looking for.
settings.Indent = true;
settings.NewLineOnAttributes = true;
These settings put the first attribute on a new line, instead of holding it on the same line as the node, and build the attributes using node.
Load, :
MyXml = XDocument.Load(filepath, LoadOptions.PreserveWhitespace);
, , .
, XmlWriter XDocument.Save, , InvalidOperationException. , .
, , xml ( )
XmlWriterSettings settings = new XmlWriterSettings();
settings.Indent = true;
settings.NewLineOnAttributes = true;
settings.OmitXmlDeclaration = true;
using (XmlWriter writer = XmlWriter.Create(filepath + "_auto", settings))
{
MyXml.Save(writer);
}