Add NormalizeWhitespace
at the end to add normalized whitespace trivia into your tree.
MethodDeclarationSyntax lMethodDeclarationSyntax =
Syntax.MethodDeclaration(
Syntax.List<AttributeListSyntax>(),
Syntax.TokenList(Syntax.Token(SyntaxKind.PublicKeyword)),
Syntax.IdentifierName("MemoryStream"),
null,
Syntax.Identifier("Serialize"),
null,
Syntax.ParameterList(),
Syntax.List<TypeParameterConstraintClauseSyntax>(),
Syntax.Block(lList))
.NormalizeWhitespace();
You can do this to any syntax node and it will return a node with all the whitespace trivia normalized throughout the tree. Remember that node trees are immutable so it will not have any effect on the already existing tree but will create a new one.