When using XmlTextWriter to write XML to a memory stream, you can specify the encoding type when create the XmlTextWriter instance.
Dim oStream As IO.MemoryStream = New IO.MemoryStream()
Dim oXmlWriter As New Xml.XmlTextWriter(oStream, System.Text.Encoding.ASCII)
oXmlWriter.WriteStartDocument()
'Top level (Parent element)
oXmlWriter.WriteStartElement("Invoice")
'Child elements, Invoice Message
oXmlWriter.WriteStartElement("InvoiceMessage")
oXmlWriter.WriteString("This is a test.")
oXmlWriter.WriteEndElement()
oXmlWriter.WriteEndElement() 'End top level element
oXmlWriter.WriteEndDocument() 'End Document
oXmlWriter.Flush() 'Write to stream
If you want to read the content from the underlining memory stream, you might be using following code:
oStream.Position = 0
Dim oContent(oStream.Length) As Byte
oStream.Read(oContent, 0, oStream.Length)
Dim encoding As System.Text.ASCIIEncoding = New System.Text.ASCIIEncoding()
MsgBox(encoding.GetString(oContent))
It works very well. However, when you change the encoding type to UTF8. The result is not what you expected. There are some strange characters in front of the XML string, which makes your XML string invalid. It seems that .NET inserted some special information into the stream for Unicode encoding.
The right way to read the content out into string is to use StreamReader object. The following code will work no problem regardless of encoding type.
oStream.Position = 0
Dim oStreamReader As System.IO.StreamReader = New System.IO.StreamReader(oStream, System.Text.Encoding.UTF8)
Dim strFreeText As String = oStreamReader.ReadToEnd()
MsgBox(strFreeText)
Dim oStream As IO.MemoryStream = New IO.MemoryStream()
Dim oXmlWriter As New Xml.XmlTextWriter(oStream, System.Text.Encoding.ASCII)
oXmlWriter.WriteStartDocument()
'Top level (Parent element)
oXmlWriter.WriteStartElement("Invoice")
'Child elements, Invoice Message
oXmlWriter.WriteStartElement("InvoiceMessage")
oXmlWriter.WriteString("This is a test.")
oXmlWriter.WriteEndElement()
oXmlWriter.WriteEndElement() 'End top level element
oXmlWriter.WriteEndDocument() 'End Document
oXmlWriter.Flush() 'Write to stream
If you want to read the content from the underlining memory stream, you might be using following code:
oStream.Position = 0
Dim oContent(oStream.Length) As Byte
oStream.Read(oContent, 0, oStream.Length)
Dim encoding As System.Text.ASCIIEncoding = New System.Text.ASCIIEncoding()
MsgBox(encoding.GetString(oContent))
It works very well. However, when you change the encoding type to UTF8. The result is not what you expected. There are some strange characters in front of the XML string, which makes your XML string invalid. It seems that .NET inserted some special information into the stream for Unicode encoding.
The right way to read the content out into string is to use StreamReader object. The following code will work no problem regardless of encoding type.
oStream.Position = 0
Dim oStreamReader As System.IO.StreamReader = New System.IO.StreamReader(oStream, System.Text.Encoding.UTF8)
Dim strFreeText As String = oStreamReader.ReadToEnd()
MsgBox(strFreeText)
Comments