There are many different ways depending on how your application works.
One method is to have a class, get the properties, then serialize & deserialize to/from XML.
So here is a class representation of your XML:
Public Class PersonName
Public Property Name() As String
End Class
Public Class User
Public Property PersonName() As PersonName
End Class
Public Class Request
Public Property User() As User
End Class
<XmlRoot(ElementName:="MyMessage")>
Public Class MyMessage
Public Property Request() As Request
Public Property CarVin() As String
End Class
Next we need to wrap the serialization code into a helper class. This is what we use in our commercial software (converted to VB):
Imports System.Xml.Serialization
Imports System.Xml
Imports System.IO
Imports System.Text
Public NotInheritable Class XmlHelper
Private Sub New()
End Sub
Public Shared Function FromClass(Of T)(data As T, _
Optional ns As XmlSerializerNamespaces = Nothing) As String
Dim response As String = String.Empty
Dim ms = New MemoryStream()
Try
ms = FromClassToStream(data)
If ms IsNot Nothing Then
ms.Position = 0
Using sr = New StreamReader(ms)
response = sr.ReadToEnd()
End Using
End If
Finally
ms.Flush()
ms.Dispose()
ms = Nothing
End Try
Return response
End Function
Public Shared Function FromClassToStream(Of T)(data As T, _
Optional ns As XmlSerializerNamespaces = Nothing) As MemoryStream
Dim stream = Nothing
If data IsNot Nothing Then
Dim settings = New XmlWriterSettings() With
{
.Encoding = Encoding.UTF8,
.Indent = True,
.ConformanceLevel = ConformanceLevel.Auto,
.CheckCharacters = True,
.OmitXmlDeclaration = False
}
Try
Dim serializer As XmlSerializer = _
XmlSerializerFactoryNoThrow.Create(GetType(T))
stream = New MemoryStream()
Using writer As XmlWriter = XmlWriter.Create(stream, settings)
serializer.Serialize(writer, data, ns)
writer.Flush()
End Using
stream.Position = 0
Catch ex As Exception
stream = Nothing
#If DEBUG Then
Debug.WriteLine(ex)
Debugger.Break()
#End If
End Try
End If
Return stream
End Function
Public Shared Function ToClass(Of T)(data As String) As T
Dim response = Nothing
If Not String.IsNullOrEmpty(data) Then
Dim settings = New XmlReaderSettings() With
{
.IgnoreWhitespace = True,
.DtdProcessing = DtdProcessing.Ignore
}
Try
Dim serializer As XmlSerializer = _
XmlSerializerFactoryNoThrow.Create(GetType(T))
Using sr = New StringReader(data)
Using reader = XmlReader.Create(sr, settings)
response = _
DirectCast(Convert.ChangeType( _
serializer.Deserialize(reader), GetType(T)), T)
End Using
End Using
Catch ex As Exception
#If DEBUG Then
Debug.WriteLine(ex)
Debugger.Break()
#End If
End Try
End If
Return response
End Function
End Class
Public NotInheritable Class XmlSerializerFactoryNoThrow
Private Sub New()
End Sub
Public Shared cache As New Dictionary(Of Type, XmlSerializer)()
Private Shared SyncRootCache As New Object()
Public Shared Function Create(type As Type) As XmlSerializer
Dim serializer As XmlSerializer
SyncLock SyncRootCache
If cache.TryGetValue(type, serializer) Then
Return serializer
End If
End SyncLock
SyncLock type
serializer = XmlSerializer.FromTypes(New Type() {type})(0)
End SyncLock
SyncLock SyncRootCache
cache(type) = serializer
End SyncLock
Return serializer
End Function
End Class
Now we are ready to serialize/deserialize the class <> XML:
Dim message As New MyMessage With
{
.CarVin = "1234566",
.Request = New Request With
{
.User = New User With
{
.PersonName = New PersonName With
{
.Name = "My Username"
}
}
}
}
Dim rawXmlMessage = XmlHelper.FromClass(message)
Console.WriteLine(rawXmlMessage)
Dim newMessage = XmlHelper.ToClass(Of MyMessage)(rawXmlMessage)