Empty lines are indented
With the following schema:
<?xml version="1.0"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="root">
<xs:complexType>
<xs:sequence>
<xs:element name="el" type="xs:string"/>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
xscgen.exe -n =MyNamespace test.xsd
cat -e MyNamespace.cs
Outputs
namespace MyNamespace^M$
{^M$
using System.Collections;^M$
using System.Collections.Generic;^M$
using System.Collections.ObjectModel;^M$
using System.Xml.Serialization;^M$
^M$
^M$
[System.CodeDom.Compiler.GeneratedCodeAttribute("XmlSchemaClassGenerator", "2.0.0.0")]^M$
[System.SerializableAttribute()]^M$
[System.Xml.Serialization.XmlTypeAttribute("root", Namespace="", AnonymousType=true)]^M$
[System.Diagnostics.DebuggerStepThroughAttribute()]^M$
[System.ComponentModel.DesignerCategoryAttribute("code")]^M$
[System.Xml.Serialization.XmlRootAttribute("root", Namespace="")]^M$
public partial class Root^M$
{^M$
^M$
[System.Xml.Serialization.XmlElementAttribute("el", Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]^M$
public string El { get; set; }^M$
}^M$
}^M$
Empty lines have trailing whitespaces.
This comes from CodeDom. Probably nothing we can do about it 🤷♂
I've opened an issue https://github.com/dotnet/corefx/issues/40811
You can use Roslyn to normalise the white space.
Using a custom FileOutputWriter
protected override void WriteFile(string path, CodeCompileUnit cu)
{
using var writer = new StringWriter();
Write(writer, cu);
var tree = CSharpSyntaxTree.ParseText(writer.ToString());
var root = tree.GetRoot();
root = root.NormalizeWhitespace();
File.WriteAllText(path, root.ToFullString());
}
@gambrose Awesome. Could you make a PR?
Do you want this to be standard behaviour to pass everything though Roslyn?
I'm not sure. Perhaps we could replace the regular CodeDomProvider with the RoslynCodeDomProvider and hope that Roslyn doesn't output empty lines with spaces. It would be great if you could try this.