我将建议将序列化作为一种输出到 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