Skip to content

Commit 6c5d372

Browse files
Fixed issue with XML Entity Formatter stream output
1 parent cfed935 commit 6c5d372

1 file changed

Lines changed: 26 additions & 17 deletions

File tree

libraries/MTConnect.NET-XML/Formatters/XmlEntityFormatter.cs

Lines changed: 26 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright (c) 2024 TrakHound Inc., All Rights Reserved.
1+
// Copyright (c) 2025 TrakHound Inc., All Rights Reserved.
22
// TrakHound Inc. licenses this file to you under the MIT license.
33

44
using MTConnect.Assets;
@@ -31,16 +31,15 @@ public FormatWriteResult Format(IDevice device, IEnumerable<KeyValuePair<string,
3131

3232
try
3333
{
34-
using (var outputStream = new MemoryStream())
34+
var outputStream = new MemoryStream();
35+
36+
// Use XmlWriter to write XML to stream
37+
using (var xmlWriter = XmlWriter.Create(outputStream, settings))
3538
{
36-
// Use XmlWriter to write XML to stream
37-
using (var xmlWriter = XmlWriter.Create(outputStream, settings))
38-
{
39-
XmlDevice.WriteXml(xmlWriter, device);
40-
xmlWriter.Flush();
39+
XmlDevice.WriteXml(xmlWriter, device);
40+
xmlWriter.Flush();
4141

42-
return FormatWriteResult.Successful(outputStream, ContentType);
43-
}
42+
return FormatWriteResult.Successful(outputStream, ContentType);
4443
}
4544
}
4645
catch { }
@@ -53,13 +52,18 @@ public FormatWriteResult Format(IObservation observation, IEnumerable<KeyValuePa
5352
{
5453
if (observation != null)
5554
{
55+
var indentOuput = GetFormatterOption<bool>(options, "indentOutput");
56+
var settings = indentOuput ? XmlFunctions.XmlWriterSettingsIndent : XmlFunctions.XmlWriterSettings;
57+
5658
try
5759
{
58-
using (var outputStream = new MemoryStream())
60+
var outputStream = new MemoryStream();
61+
62+
// Use XmlWriter to write XML to stream
63+
using (var xmlWriter = XmlWriter.Create(outputStream, settings))
5964
{
60-
// Use XmlWriter to write XML to stream
61-
var xmlWriter = XmlWriter.Create(outputStream, XmlFunctions.XmlWriterSettings);
6265
XmlObservation.WriteXml(xmlWriter, observation);
66+
xmlWriter.Flush();
6367

6468
return FormatWriteResult.Successful(outputStream, ContentType);
6569
}
@@ -74,19 +78,24 @@ public FormatWriteResult Format(IEnumerable<IObservation> observations, IEnumera
7478
{
7579
if (!observations.IsNullOrEmpty())
7680
{
81+
var indentOuput = GetFormatterOption<bool>(options, "indentOutput");
82+
var settings = indentOuput ? XmlFunctions.XmlWriterSettingsIndent : XmlFunctions.XmlWriterSettings;
83+
7784
try
7885
{
79-
using (var outputStream = new MemoryStream())
86+
var outputStream = new MemoryStream();
87+
88+
// Use XmlWriter to write XML to stream
89+
using (var xmlWriter = XmlWriter.Create(outputStream, settings))
8090
{
8191
foreach (var observation in observations)
8292
{
83-
// Use XmlWriter to write XML to stream
84-
var xmlWriter = XmlWriter.Create(outputStream, XmlFunctions.XmlWriterSettings);
8593
XmlObservation.WriteXml(xmlWriter, observation);
94+
xmlWriter.Flush();
8695
}
87-
88-
return FormatWriteResult.Successful(outputStream, ContentType);
8996
}
97+
98+
return FormatWriteResult.Successful(outputStream, ContentType);
9099
}
91100
catch { }
92101
}

0 commit comments

Comments
 (0)