From d58fb58b3acfac11fecfcdffcd91aad14170c6f8 Mon Sep 17 00:00:00 2001 From: Marc Hernandez Date: Tue, 30 Jul 2024 00:47:19 -0700 Subject: [PATCH] Title: Refactored condition checks in XmlFormatter2.cs x) This change also includes a check on the writer's state before deciding whether to write elements, improving robustness of the serialization process. --- ser/XmlFormatter2.cs | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/ser/XmlFormatter2.cs b/ser/XmlFormatter2.cs index d960bc6..93b194a 100644 --- a/ser/XmlFormatter2.cs +++ b/ser/XmlFormatter2.cs @@ -1139,12 +1139,21 @@ namespace lib if( typeCode != TypeCode.Object ) { - if( _cfg.POD == POD.Elements || forceType ) + bool writeELements = _cfg.POD == POD.Elements || forceType; + + if( !writeELements && writer is XmlTextWriter textWriter ) + { + var writeState = textWriter.WriteState; + + writeELements = writeState != WriteState.Element; + } + + if( writeELements ) writer.WriteStartElement( name ); SerializeConcrete( writer, mi, root, name, forceType ); - if( _cfg.POD == POD.Elements || forceType ) + if( writeELements ) writer.WriteEndElement(); return;