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>