473,394 Members | 1,752 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,394 software developers and data experts.

xmlTextWriter to UTF-8

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?
(it writen in utf-16 now)

..net 1.1 (VS2003)
Nov 18 '05 #1
4 18553
Hi David,
Why do you need UTF-8 encoded string?

In-memory strings are unicode (UTF-16); I don't know any other way to make
them UTF-8 without writing them to file or a memory stream.

Andrej

"David" <da*******@hotmail.com> wrote in message
news:u3**************@tk2msftngp13.phx.gbl...
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?
(it writen in utf-16 now)

.net 1.1 (VS2003)

Nov 18 '05 #2
You need to derive from StringWriter, like this:

public class StringWriterWithEncoding : StringWriter
{
Encoding encoding;

public StringWriterWithEncoding (Encoding encoding)
{
this.encoding = encoding;
}

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

Then instead of creating a StringWriter, create a
StringWriterWithEncoding, specifying the encoding you want
(Encoding.UTF8 in this case).

Jon

Nov 18 '05 #3
Andrej Tozon wrote:
Why do you need UTF-8 encoded string?

In-memory strings are unicode (UTF-16); I don't know any other way to make
them UTF-8 without writing them to file or a memory stream.


Until the XML is written out, it's irrelevant - but if you want to
write the result out in UTF-8 encoded format, it's a bit of a pain if
the XML declaration specifies that it's in UTF-16 (which it will by
default with StringWriter).

Jon

Nov 18 '05 #4
thanx, i'll try it))

"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:11**********************@g44g2000cwa.googlegr oups.com...
You need to derive from StringWriter, like this:

public class StringWriterWithEncoding : StringWriter
{
Encoding encoding;

public StringWriterWithEncoding (Encoding encoding)
{
this.encoding = encoding;
}

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

Then instead of creating a StringWriter, create a
StringWriterWithEncoding, specifying the encoding you want
(Encoding.UTF8 in this case).

Jon

Nov 18 '05 #5

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

Similar topics

3
by: Integer Software | last post by:
Hi. I have a really simple set of classes that writes 2 pathnames to a xml file. I can write the default ok. Then if I change 1 pathname to a shorter one, then rewrite the xml file, the remains...
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...
2
by: Jesper Stocholm | last post by:
I use an XmlTextWriter to generate xml data. The declaration is: System.Text.StringBuilder sb = new System.Text.StringBuilder(); System.IO.StringWriter sw = new System.IO.StringWriter(sb);...
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"?>...
2
by: Mori | last post by:
I am using the following code to manufactue xml using xmltextwriter. The xml is okay except for the extra set of start document and the outer <string> element. Where do they come from and how do...
5
by: Gilgamesh | last post by:
Hello, How do I use this class to create an XML document without saving it into a file? I need to store the XML documen into a string so it could be passed as a parameter to another class....
3
by: Mat | last post by:
Hi all. I can't understand the problem with my code... I wrote a simple test function to write an XML document and in the resulting file I find 3 strange chars before the document starting. ===...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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.