XmlSchemaClassGenerator icon indicating copy to clipboard operation
XmlSchemaClassGenerator copied to clipboard

Empty lines are indented

Open verdie-g opened this issue 6 years ago • 6 comments

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.

verdie-g avatar Sep 04 '19 14:09 verdie-g

This comes from CodeDom. Probably nothing we can do about it 🤷‍♂

mganss avatar Sep 04 '19 14:09 mganss

I've opened an issue https://github.com/dotnet/corefx/issues/40811

verdie-g avatar Sep 04 '19 15:09 verdie-g

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 avatar Oct 07 '19 12:10 gambrose

@gambrose Awesome. Could you make a PR?

mganss avatar Oct 07 '19 13:10 mganss

Do you want this to be standard behaviour to pass everything though Roslyn?

gambrose avatar Oct 07 '19 14:10 gambrose

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.

mganss avatar Oct 07 '19 14:10 mganss