diff --git a/src/Microsoft.XmlSerializer.Generator/src/Microsoft/XmlSerializer/Generator/XmlSerializationReader.cs b/src/Microsoft.XmlSerializer.Generator/src/Microsoft/XmlSerializer/Generator/XmlSerializationReader.cs index 655dfaa141f8..1513aaac38b2 100644 --- a/src/Microsoft.XmlSerializer.Generator/src/Microsoft/XmlSerializer/Generator/XmlSerializationReader.cs +++ b/src/Microsoft.XmlSerializer.Generator/src/Microsoft/XmlSerializer/Generator/XmlSerializationReader.cs @@ -1057,7 +1057,7 @@ private void WriteDerivedTypes(StructMapping mapping, bool isTypedReturn, string { for (StructMapping derived = mapping.DerivedMappings; derived != null; derived = derived.NextDerivedMapping) { - Writer.Write("else if ("); + Writer.Write("if ("); WriteQNameEqual("xsiType", derived.TypeName, derived.Namespace); Writer.WriteLine(")"); Writer.Indent++; @@ -1094,7 +1094,7 @@ private void WriteEnumAndArrayTypes() if (m is EnumMapping) { EnumMapping mapping = (EnumMapping)m; - Writer.Write("else if ("); + Writer.Write("if ("); WriteQNameEqual("xsiType", mapping.TypeName, mapping.Namespace); Writer.WriteLine(") {"); Writer.Indent++; @@ -1117,7 +1117,7 @@ private void WriteEnumAndArrayTypes() ArrayMapping mapping = (ArrayMapping)m; if (mapping.TypeDesc.HasDefaultConstructor) { - Writer.Write("else if ("); + Writer.Write("if ("); WriteQNameEqual("xsiType", mapping.TypeName, mapping.Namespace); Writer.WriteLine(") {"); Writer.Indent++; @@ -1264,10 +1264,10 @@ private void WriteLiteralStructMethod(StructMapping structMapping) Writer.Indent--; } Writer.WriteLine("}"); + Writer.WriteLine("else {"); + Writer.Indent++; WriteDerivedTypes(structMapping, !useReflection && !structMapping.TypeDesc.IsRoot, typeName); if (structMapping.TypeDesc.IsRoot) WriteEnumAndArrayTypes(); - Writer.WriteLine("else"); - Writer.Indent++; if (structMapping.TypeDesc.IsRoot) Writer.Write("return ReadTypedPrimitive(("); else @@ -1277,6 +1277,9 @@ private void WriteLiteralStructMethod(StructMapping structMapping) Writer.Indent--; Writer.WriteLine("}"); + if (structMapping.TypeDesc.IsRoot) + Writer.Indent--; + Writer.WriteLine("}"); if (structMapping.TypeDesc.IsNullable) Writer.WriteLine("if (isNull) return null;"); @@ -2217,16 +2220,28 @@ private void WriteMemberElementsIf(Member[] members, Member anyElement, string e ElementAccessor e = elements[j]; string ns = e.Form == XmlSchemaForm.Qualified ? e.Namespace : ""; if (!isSequence && e.Any && (e.Name == null || e.Name.Length == 0)) continue; - if (!firstElement || (!isSequence && count > 0)) + + if (!isSequence) { - Writer.Write("else "); + if (firstElement && count == 0) + { + Writer.WriteLine("do {"); + Writer.Indent++; + } } - else if (isSequence) + else { - Writer.Write("case "); - Writer.Write(cases.ToString(CultureInfo.InvariantCulture)); - Writer.WriteLine(":"); - Writer.Indent++; + if (!firstElement || (!isSequence && count > 0)) + { + Writer.Write("else "); + } + else if (isSequence) + { + Writer.Write("case "); + Writer.Write(cases.ToString(CultureInfo.InvariantCulture)); + Writer.WriteLine(":"); + Writer.Indent++; + } } count++; firstElement = false; @@ -2316,6 +2331,10 @@ private void WriteMemberElementsIf(Member[] members, Member anyElement, string e Writer.Write(member.ParamsReadSource); Writer.WriteLine(" = true;"); } + if (!isSequence) + { + Writer.WriteLine("break;"); + } Writer.Indent--; Writer.WriteLine("}"); } @@ -2342,10 +2361,10 @@ private void WriteMemberElementsIf(Member[] members, Member anyElement, string e if (count > 0) { if (isSequence) + { Writer.WriteLine("default:"); - else - Writer.WriteLine("else {"); - Writer.Indent++; + Writer.Indent++; + } } WriteMemberElementsElse(anyElement, elementElseString); if (count > 0) @@ -2355,7 +2374,14 @@ private void WriteMemberElementsIf(Member[] members, Member anyElement, string e Writer.WriteLine("break;"); } Writer.Indent--; - Writer.WriteLine("}"); + if (!isSequence) + { + Writer.WriteLine("} while (false);"); + } + else + { + Writer.WriteLine("}"); + } } } diff --git a/src/Microsoft.XmlSerializer.Generator/src/Microsoft/XmlSerializer/Generator/XmlSerializationWriter.cs b/src/Microsoft.XmlSerializer.Generator/src/Microsoft/XmlSerializer/Generator/XmlSerializationWriter.cs index 22c8fe63366f..215d587fcb48 100644 --- a/src/Microsoft.XmlSerializer.Generator/src/Microsoft/XmlSerializer/Generator/XmlSerializationWriter.cs +++ b/src/Microsoft.XmlSerializer.Generator/src/Microsoft/XmlSerializer/Generator/XmlSerializationWriter.cs @@ -745,7 +745,7 @@ private void WriteDerivedTypes(StructMapping mapping) for (StructMapping derived = mapping.DerivedMappings; derived != null; derived = derived.NextDerivedMapping) { string fullTypeName = derived.TypeDesc.CSharpName; - Writer.Write("else if ("); + Writer.Write("if ("); WriteTypeCompare("t", fullTypeName, derived.TypeDesc.UseReflection); Writer.WriteLine(") {"); Writer.Indent++; @@ -783,7 +783,7 @@ private void WriteEnumAndArrayTypes() { EnumMapping mapping = (EnumMapping)m; string fullTypeName = mapping.TypeDesc.CSharpName; - Writer.Write("else if ("); + Writer.Write("if ("); WriteTypeCompare("t", fullTypeName, mapping.TypeDesc.UseReflection); Writer.WriteLine(") {"); Writer.Indent++; @@ -815,7 +815,7 @@ private void WriteEnumAndArrayTypes() ArrayMapping mapping = m as ArrayMapping; if (mapping == null || m.IsSoap) continue; string fullTypeName = mapping.TypeDesc.CSharpName; - Writer.Write("else if ("); + Writer.Write("if ("); if (mapping.TypeDesc.IsArray) WriteArrayTypeCompare("t", fullTypeName, mapping.TypeDesc.ArrayElementTypeDesc.CSharpName, mapping.TypeDesc.UseReflection); else @@ -885,12 +885,12 @@ private void WriteStructMethod(StructMapping mapping) WriteTypeCompare("t", fullTypeName, mapping.TypeDesc.UseReflection); Writer.WriteLine(") {"); Writer.WriteLine("}"); + Writer.WriteLine("else {"); + Writer.Indent++; WriteDerivedTypes(mapping); if (mapping.TypeDesc.IsRoot) WriteEnumAndArrayTypes(); - Writer.WriteLine("else {"); - Writer.Indent++; if (mapping.TypeDesc.IsRoot) { Writer.WriteLine("WriteTypedPrimitive(n, ns, o, true);");