Skip to content

Commit fd23a1c

Browse files
authored
Use XmlWriter rather than XmlTextWriter (#824)
* handle the element namespace directly as a property of an element, rather than adding it to the collection of attributes * use XmlWriter rather than XmlTextWriter (see #693) * explicitly call XmlWriter.Flush() * add XmlDoc recommendation on XmlWriter usage * closes #693
1 parent ef55e3c commit fd23a1c

File tree

5 files changed

+37
-20
lines changed

5 files changed

+37
-20
lines changed

Source/Metadata/SvgDocumentMetadata.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ protected override void Render(ISvgRenderer renderer)
2525
// Do nothing. Children should NOT be rendered.
2626
}
2727

28-
protected override void WriteChildren(XmlTextWriter writer)
28+
protected override void WriteChildren(XmlWriter writer)
2929
{
3030
writer.WriteRaw(this.Content); // write out metadata as is
3131
}

Source/Scripting/SvgScript.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
using System.Xml;
1+
using System.Xml;
22

33
namespace Svg
44
{
@@ -41,7 +41,7 @@ public override SvgElement DeepCopy()
4141
return DeepCopy<SvgScript>();
4242
}
4343

44-
protected override void WriteChildren(XmlTextWriter writer)
44+
protected override void WriteChildren(XmlWriter writer)
4545
{
4646
if (!string.IsNullOrEmpty(Content))
4747
{

Source/SvgDocument.cs

+7-5
Original file line numberDiff line numberDiff line change
@@ -723,7 +723,7 @@ public virtual void RasterizeDimensions(ref SizeF size, int rasterWidth, int ras
723723
}
724724
}
725725

726-
public override void Write(XmlTextWriter writer)
726+
public override void Write(XmlWriter writer)
727727
{
728728
//Save previous culture and switch to invariant for writing
729729
var previousCulture = Thread.CurrentThread.CurrentCulture;
@@ -742,11 +742,13 @@ public override void Write(XmlTextWriter writer)
742742

743743
public void Write(Stream stream, bool useBom = true)
744744
{
745-
746-
var xmlWriter = new XmlTextWriter(stream, useBom ? Encoding.UTF8 : new UTF8Encoding(false))
745+
var settings = new XmlWriterSettings
747746
{
748-
Formatting = Formatting.Indented
747+
Encoding = useBom ? Encoding.UTF8 : new System.Text.UTF8Encoding(false),
748+
Indent = true
749749
};
750+
751+
using var xmlWriter = XmlWriter.Create(stream, settings);
750752
xmlWriter.WriteStartDocument();
751753
xmlWriter.WriteDocType("svg", "-//W3C//DTD SVG 1.1//EN", "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd", null);
752754

@@ -766,7 +768,7 @@ public void Write(string path, bool useBom = true)
766768
}
767769
}
768770

769-
protected override void WriteStartElement(XmlTextWriter writer)
771+
protected override void WriteStartElement(XmlWriter writer)
770772
{
771773
base.WriteStartElement(writer);
772774

Source/SvgElement.cs

+19-6
Original file line numberDiff line numberDiff line change
@@ -589,7 +589,7 @@ public virtual bool ShouldWriteElement()
589589
return !string.IsNullOrEmpty(this.ElementName);
590590
}
591591

592-
protected virtual void WriteStartElement(XmlTextWriter writer)
592+
protected virtual void WriteStartElement(XmlWriter writer)
593593
{
594594
if (!string.IsNullOrEmpty(this.ElementName))
595595
{
@@ -599,14 +599,14 @@ protected virtual void WriteStartElement(XmlTextWriter writer)
599599
this.WriteAttributes(writer);
600600
}
601601

602-
protected virtual void WriteEndElement(XmlTextWriter writer)
602+
protected virtual void WriteEndElement(XmlWriter writer)
603603
{
604604
if (!string.IsNullOrEmpty(this.ElementName))
605605
{
606606
writer.WriteEndElement();
607607
}
608608
}
609-
protected virtual void WriteAttributes(XmlTextWriter writer)
609+
protected virtual void WriteAttributes(XmlWriter writer)
610610
{
611611
//properties
612612
var styles = WritePropertyAttributes(writer);
@@ -662,7 +662,7 @@ protected virtual void WriteAttributes(XmlTextWriter writer)
662662
}
663663
}
664664

665-
private Dictionary<string, string> WritePropertyAttributes(XmlTextWriter writer)
665+
private Dictionary<string, string> WritePropertyAttributes(XmlWriter writer)
666666
{
667667
var styles = _styles.ToDictionary(_styles => _styles.Key, _styles => _styles.Value.Last().Value);
668668

@@ -956,7 +956,20 @@ private bool TryResolveParentAttributeValue(string attributeKey, out object pare
956956
return resolved;
957957
}
958958

959-
public virtual void Write(XmlTextWriter writer)
959+
/// <summary>
960+
/// Write this SvgElement out using a given XmlWriter.
961+
/// </summary>
962+
/// <param name="writer">The XmlWriter to use.</param>
963+
/// <remarks>
964+
/// Recommendation is to create an XmlWriter by calling a factory method,<br/>
965+
/// e.g. <see cref="XmlWriter.Create(System.IO.Stream)"/>,
966+
/// as per <a href="https://docs.microsoft.com/dotnet/api/system.xml.xmltextreader#remarks">Microsoft documentation</a>.<br/>
967+
/// <br/>
968+
/// However, unlike an <see cref="XmlTextWriter"/> created via 'new XmlTextWriter()',<br/>
969+
/// a factory-constructed XmlWriter will not flush output until it is closed<br/>
970+
/// (normally via a using statement), or unless the client explicitly calls <see cref="XmlWriter.Flush()"/>.
971+
/// </remarks>
972+
public virtual void Write(XmlWriter writer)
960973
{
961974
if (ShouldWriteElement())
962975
{
@@ -966,7 +979,7 @@ public virtual void Write(XmlTextWriter writer)
966979
}
967980
}
968981

969-
protected virtual void WriteChildren(XmlTextWriter writer)
982+
protected virtual void WriteChildren(XmlWriter writer)
970983
{
971984
if (this.Nodes.Any())
972985
{

Source/SvgExtentions.cs

+8-6
Original file line numberDiff line numberDiff line change
@@ -50,12 +50,14 @@ public static string GetXML(this SvgElement elem)
5050
try
5151
{
5252
Thread.CurrentThread.CurrentCulture = CultureInfo.InvariantCulture;
53-
using (var str = new StringWriter())
54-
using (var xml = new XmlTextWriter(str))
55-
{
56-
elem.Write(xml);
57-
result = str.ToString();
58-
}
53+
54+
var writerSettings = new XmlWriterSettings { Encoding = System.Text.Encoding.UTF8 };
55+
56+
using var str = new StringWriter();
57+
using var xml = XmlWriter.Create(str, writerSettings);
58+
elem.Write(xml);
59+
xml.Flush();
60+
result = str.ToString();
5961
}
6062
finally
6163
{

0 commit comments

Comments
 (0)