473,406 Members | 2,439 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

Stringbuilder / XmlTextWriter does not allow Encoding override

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 ?
Apr 19 '07 #1
4 9819
Rob wrote:
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.

--
Göran Andersson
_____
http://www.guffa.com
Apr 19 '07 #2
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...

"Göran Andersson" <gu***@guffa.comwrote in message
news:Ok**************@TK2MSFTNGP02.phx.gbl...
Rob wrote:
>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.

--
Göran Andersson
_____
http://www.guffa.com

Apr 19 '07 #3
Rob wrote:
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>
--

Martin Honnen --- MVP XML
http://JavaScript.FAQTs.com/
Apr 20 '07 #4
Rob wrote:
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.

--
Göran Andersson
_____
http://www.guffa.com
Apr 20 '07 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Robert Walter | last post by:
I am using the XmlTextWriter class to generate some XML however if I try to do something like string str = "cost £500"; xtw.WriteElementString("symbol", str); The £ does not get encoded to £...
5
by: Jain, Pranay Kumar | last post by:
Hello Everyone, I have written a simple app. that converts the dataset into excelspreadsheet. The App. uses the following architecture. First it generates the dataset with corresponding...
3
by: Magnus | last post by:
can anyone help me on how to create and manipulate a xmttextwriter without having to craete a physical file. I have an application that should return data in xml. But I do not want to create a...
4
by: H Lee | last post by:
Hi, I'm an XML newbie, and not sure if this is the appropriate newsgroup to post my question, so feel free to suggest other newgroups where I should post this message if this is the case. I'm...
2
by: Josh Newman | last post by:
I'm using the XMLTextWriter to create an XML document. I do not want the encoding attribure in the XML file. Instead of: <?xml version="1.0" encoding="utf-8"?> I want: <?xml version="1.0"?>...
7
by: Mike P | last post by:
I read somewhere that if you are concatenating more than 2 or 3 strings you should use StringBuilder as it will use up less resources. Does this apply when you are building up a string (for...
4
by: David | last post by:
hello... i'm using this declarations: StringWriter w = new StringWriter(); XmlTextWriter xml = new XmlTextWriter(w); how can i make the output xml (in string) to be writen in UTF-8 encoding?...
2
by: darrel | last post by:
I want to write an XML file but not to a file...just into memory, so I can then pass it to a string and stick it in a database. Seems that XMLtextWriter requires that I write to a filestream. ...
6
by: clintonG | last post by:
Can anybody make sense of this crazy and inconsistent results? // IE7 Feed Reading View disabled displays this raw XML <?xml version="1.0" encoding="utf-8" ?> <!-- AT&T HTML entities & XML...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.