Search code examples
.netxmlformattingxmlwriter

How to format an XML with closing nodes and tags on a new line?


I'm modifying some .vcrpoj files in .NET but when I save them the formatting changes (which wrecks havoc with my diff tool), the original files look like this:

<VisualStudioProject
ProjectType="Visual C++"
Version="8.00"
>
<Platforms>
    <Platform
        Name="Win32"
    />
</Platforms>
<ToolFiles>
</ToolFiles>

But when I save the changes it looks like this:

<VisualStudioProject
ProjectType="Visual C++"
Version="8.00">
<Platforms>
    <Platform
        Name="Win32" />
</Platforms>
<ToolFiles></ToolFiles>

I'm using the following XmlWritterSettings

XmlWriterSettings settings = new XmlWriterSettings();
settings.Indent = true;
settings.IndentChars = ("\t");
settings.Encoding = Encoding.UTF8;
settings.NewLineOnAttributes = true;

Is there a way to define the settings in order to match the format visual studio uses? (I need NewLineOnAttributes otherwise it's even worse).


Solution

  • I don't think you can do it with the built-in XmlWriter implementation... you could inherit from XmlTextWriter, and override the appropriate method (not sure which one it is...) to write elements with the desired format


    Here are a few XML-aware diff tools (that will compare the files based on semantics, ignoring the formatting) :

    With those tools, you won't need to worry about the XML format you generate