XML encoding this should be simple but i can`t find it

M

M.Posseth

Hello

I have this code that generates a xml string



Dim sw As New System.IO.StringWriter
Dim objXML As New XmlTextWriter(sw)
Dim strXml As String
With objXML
.Formatting = Formatting.Indented
.Indentation = 2
.WriteStartDocument()
.WriteStartElement("onlinebestellung")
.WriteStartElement("request")
.WriteElementString("cmd", "ORDER") 'INQUIRY 'ORDER
.WriteElementString("uht", "TIREP")
.WriteElementString("versandart", "0002") ' MOS
.WriteEndElement()
.WriteStartElement("positionen")
'loop voor de artikelen
.WriteStartElement("artikel")
.WriteElementString("bestellartikelnummer", "24.0110-0116.1")
.WriteElementString("bestellmenge", "1")
.WriteElementString("hersteller", "ATE")
.WriteEndElement()
'einde artikelloop
.WriteEndDocument()
.Close()
strXml = sw.ToString()
End With


now the receiving party wants this <?xml version="1.0" encoding="utf-16"
?> changed to <?xml version="1.0" encoding="iso-8859-1" ?>

how the #$#%%&^ can i do that ???

aaarghhhhh :)
 
H

Herfried K. Wagner [MVP]

M.Posseth said:
I have this code that generates a xml string

\\\
Imports System.Text
///
Dim sw As New System.IO.StringWriter
Dim objXML As New XmlTextWriter(sw)

\\\
Dim objXML As New XmlTextWriter( _
sw, _
Encoding.GetEncoding("iso-8859-1") _
)
///
 
M

M.Posseth

Herfried said:
\\\
Imports System.Text
///



\\\
Dim objXML As New XmlTextWriter( _
sw, _
Encoding.GetEncoding("iso-8859-1") _
)
///
Hello Herfried

this did not work

it says "overload resolution failed no accessible new can be called
with these arguments"

:-(
 
H

Herfried K. Wagner [MVP]

M.Posseth said:
\\\
Dim objXML As New XmlTextWriter( _
sw, _
Encoding.GetEncoding("iso-8859-1") _
)
///
[...]
it says "overload resolution failed no accessible new can be called with
these arguments"

I forget to mention that this won't work with a 'StringWriter'. You may
want to use a 'MemoryStream' instead of the 'StringWriter' or write the data
to a file directly.
 
J

Jay B. Harlow [MVP - Outlook]

M.Posseth,
now the receiving party wants this <?xml version="1.0" encoding="utf-16"
?> changed to <?xml version="1.0" encoding="iso-8859-1" ?>
The receiving party really should not care what the encoding is, UTF-16 is
equally or more valid then ISO-8859-1... (as ISO-8859-1 is a significant
subset of characters available in UTF-16.

Dim sw As New System.IO.StringWriter
StringWriter "always" uses UTF-16!

As Herfried suggests you could use a Memory Stream, alternatively you can
create an EncodedStringWriter, something like:

Public Class EncodedStringWriter
Inherits System.IO.StringWriter

Private ReadOnly m_encoding As System.Text.Encoding

Public Sub New()
MyClass.New(System.Text.Encoding.Unicode)
End Sub

Public Sub New(ByVal encoding As System.Text.Encoding)
m_encoding = encoding
End Sub

Public Overrides ReadOnly Property Encoding() As
System.Text.Encoding
Get
Return m_encoding
End Get
End Property

End Class

Then you should be able to use:

Dim sw As New EncodedStringWriter(
Encoding.GetEncoding("iso-8859-1") )
Dim objXML As New XmlTextWriter(sw)

Hope this helps
Jay
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Top