@@ -78,6 +78,9 @@ public readonly ReadOnlySpan<char> this[ReadOnlySpan<char> name]
7878 public readonly bool IsDisposed => name . IsDisposed ;
7979
8080#if NET
81+ /// <summary>
82+ /// Creates an empty XML node.
83+ /// </summary>
8184 public XMLNode ( )
8285 {
8386 name = new ( 4 ) ;
@@ -148,17 +151,16 @@ public void Dispose()
148151 public readonly override string ToString ( )
149152 {
150153 Text buffer = new ( 0 ) ;
151- ToStringFlags flags = ToStringFlags . CarriageReturn | ToStringFlags . LineFeed ;
152- ToString ( buffer , " " , flags ) ;
154+ ToString ( buffer , SerializationSettings . PrettyPrinted ) ;
153155 string str = buffer . AsSpan ( ) . ToString ( ) ;
154156 buffer . Dispose ( ) ;
155157 return str ;
156158 }
157159
158- public readonly string ToString ( string indent , ToStringFlags flags = default )
160+ public readonly string ToString ( SerializationSettings settings )
159161 {
160162 Text buffer = new ( 0 ) ;
161- ToString ( buffer , indent , flags ) ;
163+ ToString ( buffer , settings ) ;
162164 string str = buffer . AsSpan ( ) . ToString ( ) ;
163165 buffer . Dispose ( ) ;
164166 return str ;
@@ -167,8 +169,15 @@ public readonly string ToString(string indent, ToStringFlags flags = default)
167169 readonly void ISerializable . Write ( ByteWriter writer )
168170 {
169171 Text buffer = new ( 0 ) ;
170- ToStringFlags flags = ToStringFlags . CarriageReturn | ToStringFlags . LineFeed ;
171- ToString ( buffer , default , flags , 0 ) ;
172+ ToString ( buffer , SerializationSettings . PrettyPrinted , 0 ) ;
173+ writer . WriteSpan ( buffer . AsSpan ( ) ) ;
174+ buffer . Dispose ( ) ;
175+ }
176+
177+ public readonly void Write ( ByteWriter writer , SerializationSettings settings )
178+ {
179+ Text buffer = new ( 0 ) ;
180+ ToString ( buffer , settings , 0 ) ;
172181 writer . WriteSpan ( buffer . AsSpan ( ) ) ;
173182 buffer . Dispose ( ) ;
174183 }
@@ -312,21 +321,16 @@ void ISerializable.Read(ByteReader reader)
312321 }
313322 }
314323
315- public readonly void ToString ( Text destination , ReadOnlySpan < char > indent = default , ToStringFlags flags = default )
324+ public readonly void ToString ( Text destination , SerializationSettings settings = default )
316325 {
317- ToString ( destination , indent , flags , 0 ) ;
326+ ToString ( destination , settings , 0 ) ;
318327 }
319328
320- public readonly void ToString ( Text destination , string indent = "" , ToStringFlags flags = default )
321- {
322- ToString ( destination , indent . AsSpan ( ) , flags , 0 ) ;
323- }
324-
325- private readonly void ToString ( Text destination , ReadOnlySpan < char > indent , ToStringFlags flags , byte depth )
329+ private readonly void ToString ( Text destination , SerializationSettings settings , byte depth )
326330 {
327331 for ( int i = 0 ; i < depth ; i ++ )
328332 {
329- Indent ( indent ) ;
333+ settings . Indent ( destination ) ;
330334 }
331335
332336 destination . Append ( '<' ) ;
@@ -336,14 +340,17 @@ private readonly void ToString(Text destination, ReadOnlySpan<char> indent, ToSt
336340 }
337341
338342 destination . Append ( Name ) ;
339- for ( int i = 0 ; i < attributes . Count ; i ++ )
343+
344+ Span < XMLAttribute > attributesSpan = attributes . AsSpan ( ) ;
345+ for ( int i = 0 ; i < attributesSpan . Length ; i ++ )
340346 {
341347 destination . Append ( ' ' ) ;
342- XMLAttribute attribute = attributes [ i ] ;
348+ XMLAttribute attribute = attributesSpan [ i ] ;
343349 attribute . ToString ( destination ) ;
344350 }
345351
346- if ( content . Length > 0 || children . Count > 0 )
352+ Span < XMLNode > childrenSpan = children . AsSpan ( ) ;
353+ if ( content . Length > 0 || childrenSpan . Length > 0 )
347354 {
348355 if ( prologue )
349356 {
@@ -356,30 +363,37 @@ private readonly void ToString(Text destination, ReadOnlySpan<char> indent, ToSt
356363
357364 destination . Append ( '>' ) ;
358365 destination . Append ( Content ) ;
359-
360- if ( children . Count > 0 )
366+ if ( childrenSpan . Length > 0 )
361367 {
362- foreach ( XMLNode child in children )
368+ for ( int c = 0 ; c < childrenSpan . Length ; c ++ )
363369 {
364- if ( depth == 1 && ( flags & ToStringFlags . RootSpacing ) == ToStringFlags . RootSpacing )
370+ XMLNode child = childrenSpan [ c ] ;
371+ if ( ( settings . flags & SerializationSettings . Flags . SkipEmptyNodes ) == SerializationSettings . Flags . SkipEmptyNodes )
372+ {
373+ if ( child . content . Length == 0 && child . children . Count == 0 && child . attributes . Count == 0 )
374+ {
375+ continue ;
376+ }
377+ }
378+
379+ if ( depth == 1 && ( settings . flags & SerializationSettings . Flags . RootSpacing ) == SerializationSettings . Flags . RootSpacing )
365380 {
366- NewLine ( ) ;
381+ settings . NewLine ( destination ) ;
367382 }
368383
369- NewLine ( ) ;
370- child . ToString ( destination , indent , flags , depth ) ;
384+ settings . NewLine ( destination ) ;
385+ child . ToString ( destination , settings , depth ) ;
371386 }
372387
373-
374- if ( depth == 1 && ( flags & ToStringFlags . RootSpacing ) == ToStringFlags . RootSpacing )
388+ if ( depth == 1 && ( settings . flags & SerializationSettings . Flags . RootSpacing ) == SerializationSettings . Flags . RootSpacing )
375389 {
376- NewLine ( ) ;
390+ settings . NewLine ( destination ) ;
377391 }
378392
379- NewLine ( ) ;
393+ settings . NewLine ( destination ) ;
380394 for ( int i = 0 ; i < depth - 1 ; i ++ )
381395 {
382- Indent ( indent ) ;
396+ settings . Indent ( destination ) ;
383397 }
384398 }
385399
@@ -396,24 +410,6 @@ private readonly void ToString(Text destination, ReadOnlySpan<char> indent, ToSt
396410 destination . Append ( '/' ) ;
397411 destination . Append ( '>' ) ;
398412 }
399-
400- void NewLine ( )
401- {
402- if ( ( flags & ToStringFlags . CarriageReturn ) == ToStringFlags . CarriageReturn )
403- {
404- destination . Append ( '\r ' ) ;
405- }
406-
407- if ( ( flags & ToStringFlags . LineFeed ) == ToStringFlags . LineFeed )
408- {
409- destination . Append ( '\n ' ) ;
410- }
411- }
412-
413- void Indent ( ReadOnlySpan < char > indent )
414- {
415- destination . Append ( indent ) ;
416- }
417413 }
418414
419415 /// <summary>
0 commit comments