Skip to content

XDocument does not format (pretty-print), not on Save, not with XmlWriter #123340

@carlo-cs

Description

@carlo-cs

Description

I'm using the following code for formatting:

					var xdoc = XDocument.Parse( content );
					if ( xdoc.Root is not null ) {
						// ... custom code
						var sb = new StringBuilder();
						var settings = new XmlWriterSettings {
							Indent = true,
							IndentChars = "\t",
							OmitXmlDeclaration = true,
							NewLineOnAttributes = false,
							Encoding = Encoding.UTF8,
							WriteEndDocumentOnClose = true,
							NewLineChars = CS.CrLf,
							NewLineHandling = NewLineHandling.Replace
						};
						using ( var writer = XmlWriter.Create( sb, settings ) ) {
							xdoc.Save( writer );
						}
						content = sb.ToString();
					}
					content = StructureCheck.FixXmlToHtml( content );

The result, in 'content', is not formatted!
It contains strings like the following:
"  

"

So it is not formatted at all.

Before using the shown code, I also used "xdoc.ToString()", with the same, bad results.

Kind regards
Carlo

Reproduction Steps

See above.

Expected behavior

There should be a formatted, indented output, like:

<table class="page_table">
	<tbody>
		<tr>
			<td>
				&nbsp;
				<br />
				per E-Mail: {$Contact_Email}
			</td>
		</tr>
		<tr><td>&nbsp;</td></tr>
		<tr><td align="right">{$Date_Long} {$User_Code}&nbsp;</td></tr>
		<tr><td>&nbsp;</td></tr>
		<tr><td>&nbsp;</td></tr>
		<tr><td>&nbsp;</td></tr>
	</tbody>
</table>

Actual behavior

Flat output, like:

"  

"

Regression?

No response

Known Workarounds

None

Configuration

Console Application

DotNET Framework 9.0, on Windows Server.

Microsoft Windows [Version 10.0.20348.4405]

Microsoft Visual Studio Community 2022 (64-bit)
Version 17.14.19

Other information

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    area-System.XmluntriagedNew issue has not been triaged by the area owner

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions