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
44using MTConnect . Configurations ;
@@ -57,41 +57,39 @@ public IDevicesResponseDocument ToDocument()
5757 return document ;
5858 }
5959
60+ /// <exception cref="XmlException">XML Exception thrown during Serialization</exception>
6061 public static IDevicesResponseDocument FromXml ( byte [ ] xmlBytes )
6162 {
6263 if ( xmlBytes != null && xmlBytes . Length > 0 )
6364 {
64- try
65- {
66- // Clean whitespace and Encoding Marks (BOM)
67- var bytes = XmlFunctions . SanitizeBytes ( xmlBytes ) ;
65+ // Clean whitespace and Encoding Marks (BOM)
66+ var bytes = XmlFunctions . SanitizeBytes ( xmlBytes ) ;
6867
69- var xml = Encoding . UTF8 . GetString ( bytes ) ;
70- var version = MTConnectVersion . Get ( xml ) ;
68+ var xml = Encoding . UTF8 . GetString ( bytes ) ;
69+ var version = MTConnectVersion . Get ( xml ) ;
7170
72- xml = xml . Trim ( ) ;
73- xml = Namespaces . Clear ( xml ) ;
71+ xml = xml . Trim ( ) ;
72+ xml = Namespaces . Clear ( xml ) ;
7473
75- using ( var textReader = new StringReader ( xml ) )
74+ using ( var textReader = new StringReader ( xml ) )
75+ {
76+ using ( var xmlReader = XmlReader . Create ( textReader ) )
7677 {
77- using ( var xmlReader = XmlReader . Create ( textReader ) )
78+ var xmlDocument = ( XmlDevicesResponseDocument ) _serializer . Deserialize ( xmlReader ) ;
79+ if ( xmlDocument != null )
7880 {
79- var xmlDocument = ( XmlDevicesResponseDocument ) _serializer . Deserialize ( xmlReader ) ;
80- if ( xmlDocument != null )
81- {
82- xmlDocument . Version = version ;
83- var document = xmlDocument . ToDocument ( ) ;
84- return document ;
85- }
81+ xmlDocument . Version = version ;
82+ var document = xmlDocument . ToDocument ( ) ;
83+ return document ;
8684 }
8785 }
8886 }
89- catch { }
9087 }
9188
9289 return null ;
9390 }
9491
92+ /// <exception cref="XmlException">XML Exception thrown during Serialization</exception>
9593 public static byte [ ] ToXmlBytes (
9694 IDevicesResponseDocument document ,
9795 IEnumerable < NamespaceConfiguration > extendedSchemas = null ,
@@ -102,27 +100,24 @@ public static byte[] ToXmlBytes(
102100 {
103101 if ( document != null && document . Header != null )
104102 {
105- try
103+ using ( var stream = new MemoryStream ( ) )
106104 {
107- using ( var stream = new MemoryStream ( ) )
108- {
109- // Set the XmlWriterSettings to use
110- var xmlWriterSettings = indent ? XmlFunctions . XmlWriterSettingsIndent : XmlFunctions . XmlWriterSettings ;
105+ // Set the XmlWriterSettings to use
106+ var xmlWriterSettings = indent ? XmlFunctions . XmlWriterSettingsIndent : XmlFunctions . XmlWriterSettings ;
111107
112- // Use XmlWriter to write XML to stream
113- using ( var xmlWriter = XmlWriter . Create ( stream , xmlWriterSettings ) )
114- {
115- WriteXml ( xmlWriter , document , indent , outputComments , styleSheet , extendedSchemas ) ;
116- return stream . ToArray ( ) ;
117- }
108+ // Use XmlWriter to write XML to stream
109+ using ( var xmlWriter = XmlWriter . Create ( stream , xmlWriterSettings ) )
110+ {
111+ WriteXml ( xmlWriter , document , indent , outputComments , styleSheet , extendedSchemas ) ;
112+ return stream . ToArray ( ) ;
118113 }
119114 }
120- catch { }
121115 }
122116
123117 return null ;
124118 }
125119
120+ /// <exception cref="XmlException">XML Exception thrown during Serialization</exception>
126121 public static Stream ToXmlStream (
127122 IDevicesResponseDocument document ,
128123 IEnumerable < NamespaceConfiguration > extendedSchemas = null ,
@@ -133,26 +128,23 @@ public static Stream ToXmlStream(
133128 {
134129 if ( document != null && document . Header != null )
135130 {
136- try
137- {
138- var outputStream = new MemoryStream ( ) ;
131+ var outputStream = new MemoryStream ( ) ;
139132
140- // Set the XmlWriterSettings to use
141- var xmlWriterSettings = indent ? XmlFunctions . XmlWriterSettingsIndent : XmlFunctions . XmlWriterSettings ;
133+ // Set the XmlWriterSettings to use
134+ var xmlWriterSettings = indent ? XmlFunctions . XmlWriterSettingsIndent : XmlFunctions . XmlWriterSettings ;
142135
143- // Use XmlWriter to write XML to stream
144- using ( var xmlWriter = XmlWriter . Create ( outputStream , xmlWriterSettings ) )
145- {
146- WriteXml ( xmlWriter , document , indent , outputComments , styleSheet , extendedSchemas ) ;
147- return outputStream ;
148- }
136+ // Use XmlWriter to write XML to stream
137+ using ( var xmlWriter = XmlWriter . Create ( outputStream , xmlWriterSettings ) )
138+ {
139+ WriteXml ( xmlWriter , document , indent , outputComments , styleSheet , extendedSchemas ) ;
140+ return outputStream ;
149141 }
150- catch { }
151142 }
152143
153144 return null ;
154145 }
155146
147+ /// <exception cref="XmlException">XML Exception thrown during Serialization</exception>
156148 public static void WriteXml (
157149 XmlWriter writer ,
158150 IDevicesResponseDocument document ,
0 commit comments