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.
This commit is contained in:
Marc Hernandez 2024-07-30 00:47:19 -07:00
parent b66bc4e5ed
commit d58fb58b3a

View File

@ -1139,12 +1139,21 @@ namespace lib
if( typeCode != TypeCode.Object ) 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 ); writer.WriteStartElement( name );
SerializeConcrete( writer, mi, root, name, forceType ); SerializeConcrete( writer, mi, root, name, forceType );
if( _cfg.POD == POD.Elements || forceType ) if( writeELements )
writer.WriteEndElement(); writer.WriteEndElement();
return; return;