Stringbuilder / XmlTextWriter does not allow Encoding override


R

Rob

VB.net

I need to create xml using Windows-1252 encoding...

So I try...

Dim sw as new StringWriter
Dim tw as new XmlTextWriter(sw, Encoding(1252))

But I get an error stating that...

System.IO.StringWriter cannot be converted to string
And
System.IO.StringWriter cannot be converted to System.IO.Stream

Any ideas ?
 
Ad

Advertisements

?

=?ISO-8859-1?Q?G=F6ran_Andersson?=

Rob said:
VB.net

I need to create xml using Windows-1252 encoding...

So I try...

Dim sw as new StringWriter
Dim tw as new XmlTextWriter(sw, Encoding(1252))

But I get an error stating that...

System.IO.StringWriter cannot be converted to string
And
System.IO.StringWriter cannot be converted to System.IO.Stream

Any ideas ?

If you want the XML to be encoded, you have to write it to a binary
stream (e.g. a MemoryStream). Writing it to a StringWriter doesn't
encode it.
 
R

Rob

I am having a heck of a time finding an example that works...
I see code that has "XmlWriterSettings", yet when I try it, intellisense
does not validiate that such an item even exists...

and I did set the Xml namespace...
 
M

Martin Honnen

Rob said:
VB.net

I need to create xml using Windows-1252 encoding...

So I try...

Dim sw as new StringWriter
Dim tw as new XmlTextWriter(sw, Encoding(1252))

But I get an error stating that...

System.IO.StringWriter cannot be converted to string
And
System.IO.StringWriter cannot be converted to System.IO.Stream

A .NET string is always a sequence of Unicode characters so it does not
make much sense to create a string with XML markup but to require it is
encoded as Windows-1252.
If you nevertheless insist of having <?xml version="1.0"
encoding="Windows-1252"?> in a string with XML markup then you can do so
by subclassing StringWriter and overriding the Encoding property, here
is a .NET 2.0 C# example, hopefully you can transcribe that to VB yourself:

public class StringWriterWithEncoding : StringWriter
{
private Encoding MyEncoding;

public StringWriterWithEncoding(Encoding encoding)
: base()
{
MyEncoding = encoding;
}

public override Encoding Encoding
{
get
{
return MyEncoding;
}
}
}

then use that class as e.g.

StringWriterWithEncoding stringWriter = new
StringWriterWithEncoding(Encoding.GetEncoding(1252));
using (XmlWriter xmlWriter = XmlWriter.Create(stringWriter))
{
xmlWriter.WriteStartDocument();
xmlWriter.WriteStartElement("root");
xmlWriter.WriteElementString("foo", "bar");
xmlWriter.WriteEndDocument();
}
Console.WriteLine(stringWriter.ToString());

which outputs e.g.

<?xml version="1.0" encoding="Windows-1252"?><root><foo>bar</foo></root>
 
Ad

Advertisements

?

=?ISO-8859-1?Q?G=F6ran_Andersson?=

Rob said:
I am having a heck of a time finding an example that works...
I see code that has "XmlWriterSettings", yet when I try it, intellisense
does not validiate that such an item even exists...

and I did set the Xml namespace...

The XmlWriterSettings class only exists in framework 2.0.
 

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

Top