Imports System.Xml.Serialization
Imports System.Collections.Generic
<XmlElement("var")> Public name As String
<XmlElement("troublecodes")> Public troubleCodes As TroubleCodes
Public Class TroubleCodes
<XmlArrayItem("troublecode")> Public troubleCode() As TroubleCode
<XmlArrayItem("statusbyte")> Public statusByte() As StatusByte
<XmlElement("one")> Public one As String
<XmlElement("two")> Public two As String
<XmlElement("three")> Public three As String
<XmlElement("four")> Public four As String
Dim myVar = new MyVar() With { _
.troubleCodes = new TroubleCodes() With { _
.troubleCode = { new TroubleCode With { .one = "one", .two = "two" } }, _
.statusByte = { new StatusByte With { .three = "three", .four = "four" } } _
Dim newXml = myVar.GetXml(true)
Console.WriteLine(newXml)
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)
<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()
Using textWriter = New StringWriter()
Dim settings = New XmlWriterSettings() With { _
Using xmlWriter = System.Xml.XmlWriter.Create(textWriter, settings)
Dim serializer as New XmlSerializer(obj.GetType())
serializer.Serialize(xmlWriter, obj, ns)
Return textWriter.ToString()