XML に出力する簡単な方法としてシリアライゼーションを提案するつもりでした。簡単な例を次に示します。
最初にクラスを作成します
Public Class Login
Public Property USERNAME() As String
Get
Return _USERNAME
End Get
Set(ByVal value As String)
_USERNAME = value
End Set
End Property
Private _USERNAME As String
Public Property PASSWORD() As String
Get
Return _PASSWORD
End Get
Set(ByVal value As String)
_PASSWORD = value
End Set
End Property
Private _PASSWORD As String
End Class
Public Class Body
Public Property Login() As Login
Get
Return _login
End Get
Set(ByVal value As LoginClass)
_login = value
End Set
End Property
Private _login As Login = New Login()
End Class
Public Class Envelope
Public Property Body() As Body
Get
Return _body
End Get
Set(ByVal value As Body)
_body = value
End Set
End Property
Private _body As Body = New Body()
End Class
次に、エンベロープ オブジェクトを作成してデータを入力し、シリアル化します。
Dim envelope As New Envelope()
envelope.Body.Login.USERNAME = "username"
envelope.Body.Login.PASSWORD = "Sm@rt123"
Dim stream As MemoryStream = New MemoryStream()
Dim textWriter As XmlTextWriter = New XmlTextWriter(stream, New System.Text.UTF8Encoding(False))
Dim serializer As XmlSerializer = New XmlSerializer(GetType(Envelope))
Dim namespaces As XmlSerializerNamespaces = New XmlSerializerNamespaces()
namespaces.Add("", "")
serializer.Serialize(textWriter, envelope, namespaces)
Dim doc As XmlDocument = New XmlDocument()
doc.LoadXml(Encoding.UTF8.GetString(stream.ToArray()))
Dim xmlText As String = doc.SelectSingleNode("Envelope").OuterXml