Skip to content

Commit

Permalink
56014 xml writer.create perf regression (#56524)
Browse files Browse the repository at this point in the history
* Use non-indented XmlWriter to avoid perf hit.
* Add test for issue #46974 which was also fixed by original PR that caused this regression.
  • Loading branch information
StephenMolloy committed Aug 4, 2021
1 parent 28818b9 commit 880e67d
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -324,7 +324,7 @@ public void Serialize(TextWriter textWriter, object? o)
[RequiresUnreferencedCode(TrimSerializationWarning)]
public void Serialize(TextWriter textWriter, object? o, XmlSerializerNamespaces? namespaces)
{
XmlWriter xmlWriter = XmlWriter.Create(textWriter, new XmlWriterSettings() { Indent = true });
XmlWriter xmlWriter = XmlWriter.Create(textWriter);
Serialize(xmlWriter, o, namespaces);
}

Expand All @@ -337,7 +337,7 @@ public void Serialize(Stream stream, object? o)
[RequiresUnreferencedCode(TrimSerializationWarning)]
public void Serialize(Stream stream, object? o, XmlSerializerNamespaces? namespaces)
{
XmlWriter xmlWriter = XmlWriter.Create(stream, new XmlWriterSettings() { Indent = true });
XmlWriter xmlWriter = XmlWriter.Create(stream);
Serialize(xmlWriter, o, namespaces);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -502,6 +502,21 @@ public static void Xml_WithXElementWithNestedXElement()
VerifyXElementObject((XElement)original.e1.FirstNode, (XElement)actual.e1.FirstNode);
}

[Fact]
public static void Xml_WithXElementWithEmptyNestedElement()
{
var original = new WithXmlElement(true);
original.xml.InnerXml = "<empty></empty>";

MemoryStream ms = new MemoryStream();
new XmlSerializer(typeof(WithXmlElement)).Serialize(ms, original);

ms.Position = 0;
StreamReader sr = new StreamReader(ms);
string output = sr.ReadToEnd();
Assert.Contains("<empty></empty>", output); // Self-closed, or completely empty is OK. No added space.
}

[Fact]
public static void Xml_WithArrayOfXElement()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -767,6 +767,19 @@ public WithXElement(bool init)
}
}

public class WithXmlElement
{
public XmlElement xml;

public WithXmlElement() { }

public WithXmlElement(bool init)
{
var doc = new XmlDocument();
xml = doc.CreateElement("Element1");
}
}

public class WithXElementWithNestedXElement
{
public XElement e1;
Expand Down

0 comments on commit 880e67d

Please sign in to comment.