Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -89,5 +89,15 @@ public static bool IgnoreObsoleteMembers
return SwitchesHelpers.GetCachedSwitchValue("Switch.System.Xml.IgnoreObsoleteMembers", ref s_ignoreObsoleteMembers);
}
}

private static int s_useLegacyEmptyXmlElementDeserialization;
public static bool UseLegacyEmptyXmlElementDeserialization
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
get
{
return SwitchesHelpers.GetCachedSwitchValue("Switch.System.Xml.UseLegacyEmptyXmlElementDeserialization", ref s_useLegacyEmptyXmlElementDeserialization);
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -905,6 +905,11 @@ protected XmlQualifiedName ReadElementQualifiedName()
if (wrapped)
{
if (ReadNull()) return null;
if (!LocalAppContextSwitches.UseLegacyEmptyXmlElementDeserialization && _r.IsEmptyElement)
{
_r.Skip();
return null;
}
_r.ReadStartElement();
_r.MoveToContent();
if (_r.NodeType != XmlNodeType.EndElement)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3666,4 +3666,32 @@ public static void SerializePrimitiveXmlTextAttributeOnDerivedClass()
var actual = SerializeAndDeserialize(value, "<?xml version=\"1.0\"?><PrimiveAttributeTestDerived xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\">5</PrimiveAttributeTestDerived>");
Assert.Equal(value.Number, actual.Number);
}

[Theory]
[InlineData(@"<TypeWithXmlElementMemberAndSibling xmlns:xsi=""http://www.w3.org/2001/XMLSchema-instance"" xmlns:xsd=""http://www.w3.org/2001/XMLSchema""><Description><p>text</p></Description><Name>Test</Name></TypeWithXmlElementMemberAndSibling>", "Test", true, "p", "text")]
[InlineData(@"<TypeWithXmlElementMemberAndSibling xmlns:xsi=""http://www.w3.org/2001/XMLSchema-instance"" xmlns:xsd=""http://www.w3.org/2001/XMLSchema""><Description /><Name>Test</Name></TypeWithXmlElementMemberAndSibling>", "Test", false, null, null)]
[InlineData(@"<TypeWithXmlElementMemberAndSibling xmlns:xsi=""http://www.w3.org/2001/XMLSchema-instance"" xmlns:xsd=""http://www.w3.org/2001/XMLSchema""><Description/><Name>Test</Name></TypeWithXmlElementMemberAndSibling>", "Test", false, null, null, false)]
[InlineData(@"<TypeWithXmlElementMemberAndSibling xmlns:xsi=""http://www.w3.org/2001/XMLSchema-instance"" xmlns:xsd=""http://www.w3.org/2001/XMLSchema""><Description></Description><Name>Test</Name></TypeWithXmlElementMemberAndSibling>", "Test", false, null, null, false)]
[InlineData(@"<TypeWithXmlElementMemberAndSibling xmlns:xsi=""http://www.w3.org/2001/XMLSchema-instance"" xmlns:xsd=""http://www.w3.org/2001/XMLSchema""><Description><p>text</p></Description><Name>Test</Name></TypeWithXmlElementMemberAndSibling>", "Test", true, "p", "text", true)]
[InlineData(@"<TypeWithXmlElementMemberAndSibling xmlns:xsi=""http://www.w3.org/2001/XMLSchema-instance"" xmlns:xsd=""http://www.w3.org/2001/XMLSchema""><Description /><Name>Test</Name></TypeWithXmlElementMemberAndSibling>", null, true, "Name", "Test", true)]
[InlineData(@"<TypeWithXmlElementMemberAndSibling xmlns:xsi=""http://www.w3.org/2001/XMLSchema-instance"" xmlns:xsd=""http://www.w3.org/2001/XMLSchema""><Description></Description><Name>Test</Name></TypeWithXmlElementMemberAndSibling>", "Test", false, null, null, true)]
public static void Xml_XmlElementMember_EmptyElement_SiblingNotConsumed(string xml, string? expectedName, bool expectDescription, string? expectedDescriptionName, string? expectedDescriptionInnerXml, bool? compatSwitch = null)
{
using (var appContextScope = compatSwitch.HasValue ? new XmlSerializerAppContextSwitchScope("Switch.System.Xml.UseLegacyEmptyXmlElementDeserialization", compatSwitch.Value) : null)
{
var serializer = new XmlSerializer(typeof(TypeWithXmlElementMemberAndSibling));
TypeWithXmlElementMemberAndSibling obj = (TypeWithXmlElementMemberAndSibling)serializer.Deserialize(new StringReader(xml));
Assert.Equal(expectedName, obj.Name);
if (expectDescription)
{
Assert.NotNull(obj.Description);
Assert.Equal(expectedDescriptionName, obj.Description.Name);
Assert.Equal(expectedDescriptionInnerXml, obj.Description.InnerXml);
}
else
{
Assert.Null(obj.Description);
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -836,6 +836,12 @@ public WithListOfXElement(bool init)
}
}

public class TypeWithXmlElementMemberAndSibling
{
public XmlElement Description { get; set; }
public string Name { get; set; }
}

public class BaseType
{
public virtual string Name1 { get; set; }
Expand Down
Loading