Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use XmlWriter.Create instead of XmlTextWriter #54949

Merged
Original file line number Diff line number Diff line change
Expand Up @@ -317,9 +317,7 @@ public void Serialize(TextWriter textWriter, object? o)
[RequiresUnreferencedCode(TrimSerializationWarning)]
public void Serialize(TextWriter textWriter, object? o, XmlSerializerNamespaces? namespaces)
{
XmlTextWriter xmlWriter = new XmlTextWriter(textWriter);
xmlWriter.Formatting = Formatting.Indented;
xmlWriter.Indentation = 2;
XmlWriter xmlWriter = XmlWriter.Create(textWriter, new XmlWriterSettings() { Indent = true });
Serialize(xmlWriter, o, namespaces);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why explicitly set CheckCharacters and IndentChars? You are seeting them to their default values. Indent is the only property which you are changing from the default.

}

Expand All @@ -332,9 +330,7 @@ public void Serialize(Stream stream, object? o)
[RequiresUnreferencedCode(TrimSerializationWarning)]
public void Serialize(Stream stream, object? o, XmlSerializerNamespaces? namespaces)
{
XmlTextWriter xmlWriter = new XmlTextWriter(stream, null);
xmlWriter.Formatting = Formatting.Indented;
xmlWriter.Indentation = 2;
XmlWriter xmlWriter = XmlWriter.Create(stream, new XmlWriterSettings() { Indent = true });
Serialize(xmlWriter, o, namespaces);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same for this, skip setting values to the defaults.

}

Expand Down Expand Up @@ -421,10 +417,7 @@ private XmlMapping GetMapping()
[RequiresUnreferencedCode(TrimDeserializationWarning)]
public object? Deserialize(Stream stream)
{
XmlTextReader xmlReader = new XmlTextReader(stream);
xmlReader.WhitespaceHandling = WhitespaceHandling.Significant;
xmlReader.Normalization = true;
xmlReader.XmlResolver = null;
XmlReader xmlReader = XmlReader.Create(stream, new XmlReaderSettings() { IgnoreWhitespace = true });
return Deserialize(xmlReader, null);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,13 @@ public static void Xml_StringAsRoot()
"<?xml version=\"1.0\"?><string>Hello World! \u6F22 \u00F1</string>"));
}

[Fact]
public static void Xml_StringWithNullChar()
{
Assert.Throws<InvalidOperationException>(() => SerializeWithDefaultValue<string>("Sample\0String", null));
Assert.Throws<InvalidOperationException>(() => DeserializeFromXmlString<string>("<?xml version=\"1.0\"?><string>Sample&#x0;String</string>"));
}

[Fact]
public static void Xml_UintAsRoot()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1726,6 +1726,17 @@ private static bool SerializeWithDefaultValue<T>(T value, string baseline)
}
}

private static T DeserializeFromXmlString<T>(string xmlString)
{
XmlSerializer serializer = new XmlSerializer(typeof(T));
using (Stream ms = GenerateStreamFromString(xmlString))
{
T value = (T)serializer.Deserialize(ms);
return value;
}

}

[Fact]
public static void Xml_TypeWithMismatchBetweenAttributeAndPropertyType()
{
Expand Down