https://stackoverflow.com/questions/69502162/preserve-xml-xsd-schemalocation-when-deserializing-then-serializing

VbNet · Console

/Program.vb

Imports System
Imports System.Collections.Generic
Imports System.Collections
Imports System.IO
Imports System.Text
Imports System.Collections.ObjectModel
Imports System.Linq
Imports System.Reflection

Imports System.Xml.Serialization
Imports System.Xml
Imports System.Xml.Linq
Imports System.Xml.XPath
Imports System.Xml.Schema

' https://stackoverflow.com/questions/69502162/preserve-xml-xsd-schemalocation-when-deserializing-then-serializing

<XmlRoot("StationSetpoints")>
Public Partial Class XmlStationSetpoints
    <XmlElement("Setpoint")>
    Public Property Setpoints As List(Of XmlStationSetpointsSetpoint)
End Class
Public Partial Class XmlStationSetpoints
    <XmlAttribute("schemaLocation", Namespace:="http://www.w3.org/2001/XMLSchema-instance")>
	Public Property xsiSchemaLocation As String = "http://www.w3schools.com StationSetpoints.xsd"

End Class

<Serializable>
Public Class XmlStationSetpointsSetpoint
    <XmlElement("Point")>
    Public Property Points As List(Of XmlStationSetpointsSetpointPoint)
    ' etc...
End Class

Public Class XmlStationSetpointsSetpointPoint
	<XmlAttribute>
	Public Property Order As String
	<XmlAttribute>
	Public Property InstrumentName As String
	<XmlAttribute>
	Public Property Value As String
End Class
			
Public Module Module1
	Public Sub Main()
		Dim xml as String = GetXml()

		Console.WriteLine(xml)
						
		Dim model = xml.LoadFromXml(Of XmlStationSetpoints)()
							
		Dim newXml = model.GetXml()
							
		Console.WriteLine(newXml)
	End Sub

	Function GetXml() As String
		Dim xml as String = <StationSetpoints
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:xsd="http://www.w3.org/2001/XMLSchema"
    xsi:schemaLocation="http://www.w3schools.com StationSetpoints.xsd">
  <Setpoint PartNumber="108022">
    <Point InstrumentName="PD Stage" Value="10"/>
  </Setpoint>
  <Setpoint PartNumber="107983">
    <Point Order="2" InstrumentName="PD Stage" Value="7.5"/>
  </Setpoint>
</StationSetpoints>.ToString()
		Return xml
	End Function
End Module

Public Module XmlSerializationHelper
	<System.Runtime.CompilerServices.Extension> _
	Public Function LoadFromXML(Of T)(xmlString As String) As T
		Using reader As New StringReader(xmlString)
			Return DirectCast(New XmlSerializer(GetType(T)).Deserialize(reader), T)
		End Using
	End Function

	<System.Runtime.CompilerServices.Extension> _
	Public Function GetXml(Of T)(obj As T, Optional omitStandardNamespaces As Boolean = False) As String
		Dim ns As XmlSerializerNamespaces = Nothing
		If omitStandardNamespaces Then
			ns = New XmlSerializerNamespaces()
				' Disable the xmlns:xsi and xmlns:xsd lines.
			ns.Add("", "")
		End If
		Return obj.GetXml(ns)
	End Function

	<System.Runtime.CompilerServices.Extension> _
	Public Function GetXml(Of T)(obj As T, ns as XmlSerializerNamespaces) As String
		Using textWriter = New StringWriter()
			Dim settings = New XmlWriterSettings() With { _
				.Indent = True _
			}
			' For cosmetic purposes.
			Using xmlWriter = System.Xml.XmlWriter.Create(textWriter, settings)
				Dim serializer as New XmlSerializer(obj.GetType()) 
				serializer.Serialize(xmlWriter, obj, ns)
			End Using
			Return textWriter.ToString()
		End Using
	End Function

	<System.Runtime.CompilerServices.Extension> _
	Public Function GetOuterXml(node as XmlNode, Optional indent as Boolean = true) as String
		Using textWriter = New StringWriter()
			' For cosmetic purposes.
			Dim settings = New XmlWriterSettings() With { _
				.Indent = indent, _
				.OmitXmlDeclaration = true _
			}
			Using xmlWriter = System.Xml.XmlWriter.Create(textWriter, settings)
				node.WriteTo(xmlWriter)
			End Using
			Return textWriter.ToString()
		End Using		
	End Function
End Module
;