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

XML Encoding Question

Hi there,

how do you set the encoding format of an XML string? When I
was outputting the XML to a file you can specify the encoding format like
so:

XmlTextWriter myWriter;

myWriter = new XmlTextWriter(myXMLFile, System.Text.Encoding.UTF8);

XmlSerializer serializer = new XmlSerializer(typeof(@event));

serializer.Serialize(myWriter, myEvent);

but I now want to output the XML as a string:

XmlSerializer serialiser = new XmlSerializer(typeof(@event));

TextWriter textWriter = new StringWriter();

XmlWriter writer = new XmlTextWriter(textWriter);

serialiser.Serialize(writer, myEvent);

strData = textWriter.ToString();

there is no way of setting the encoding format and it is set to UTF16
instead of UTF8 as required by the customer.

Any ideas?
Nov 16 '05 #1
5 5230
Waldy <wa***@notmail.com> wrote:
how do you set the encoding format of an XML string? When I
was outputting the XML to a file you can specify the encoding format like
so:

XmlTextWriter myWriter;

myWriter = new XmlTextWriter(myXMLFile, System.Text.Encoding.UTF8);

XmlSerializer serializer = new XmlSerializer(typeof(@event));

serializer.Serialize(myWriter, myEvent);

but I now want to output the XML as a string:

XmlSerializer serialiser = new XmlSerializer(typeof(@event));

TextWriter textWriter = new StringWriter();

XmlWriter writer = new XmlTextWriter(textWriter);

serialiser.Serialize(writer, myEvent);

strData = textWriter.ToString();

there is no way of setting the encoding format and it is set to UTF16
instead of UTF8 as required by the customer.


Use something like this:

public class StringWriterWithEncoding : StringWriter
{
Encoding encoding;

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

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

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #2
That's it! Thanks Jon.
Nov 16 '05 #3
Got here via a Google search... I am having a problem with the
disable-output-escaping. I have a Jscript client side script that has a
script like

<script language="JScript"><xsl:text
disable-output-escaping="yes"><![CDATA[
alert("hey, here's a < and a > ");
]]></xsl:text></script>

My output is ending up as alert("hey, here's a &lt; and a &gt; ");
which causes IE to throw an error on the page.

I am trying to do the same thing as the original poster (writing to a
string) . Will changing the encoding effect this problem ?

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 16 '05 #4

"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...

Use something like this:

public class StringWriterWithEncoding : StringWriter
{
Encoding encoding;

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

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


Hi Jon,
this didn't work. I thought it had done, because the encoding
tag was set to "utf-8" although if you look at the data in hexadecimal, you
can see nulls between each character, so it must still be UTF-16.
Nov 16 '05 #5
Waldy <wa***@notmail.com> wrote:
Use something like this:

public class StringWriterWithEncoding : StringWriter
{
Encoding encoding;

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

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


this didn't work. I thought it had done, because the encoding
tag was set to "utf-8" although if you look at the data in hexadecimal, you
can see nulls between each character, so it must still be UTF-16.


The data that a StringWriter outputs is just character data. To get
*bytes* you need to be using a StreamWriter or something similar - and
you'll need to use UTF-8 there as well.

Could you post a short but complete program which demonstrates the
problem?

See http://www.pobox.com/~skeet/csharp/complete.html for details of
what I mean by that.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #6

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

Similar topics

2
by: Mark | last post by:
Sorry about the last... Anyway, here's the question: I've been working on some C# routines to process strings in and out of various encodings. The hope is that I can just let the user type in...
7
by: polarz | last post by:
I wrote a front end to a command line mp4 music tagger that reads my playlist files and gets info such as artist, title, album, etc, etc. I use the info to catalog, tag, sort, etc, my files. I've...
4
by: Christina | last post by:
Hey Guys, Currently, I am using the below code: Dim oReqDoc as XmlDocument Dim requiredBytes As Byte() requiredBytes = System.Text.UTF8Encoding.UTF8.GetBytes(oReqDoc.InnerXml). Here, I am...
4
by: George | last post by:
Hi, I am puzzled by the following and seeking some assistance to help me understand what happened. I have very limited encoding knowledge. Our SAP system writes out a text file which includes...
4
by: Provost Zakharov | last post by:
Hello, I just needed some help on how the DOM is encoded by the IE parser. As per the MSDN page, http://msdn.microsoft.com/workshop/author/dhtml/reference/charsets/charset4.asp ,server encodings...
3
by: mortb | last post by:
1. How do I determine which encoding a xmldocument or xmlreader uses when opening a document? I'm not just talking about the <?xml encoding="utf-8"?attribute, but the actual encoding of the...
23
by: Allan Ebdrup | last post by:
I hava an ajax web application where i hvae problems with UTF-8 encoding oc chineese chars. My Ajax webapplication runs in a HTML page that is UTF-8 Encoded. I copy and paste some chineese chars...
1
by: ujjwaltrivedi | last post by:
Hey guys, Can anyone tell me how to create a text file with Unicode Encoding. In am using FileStream Finalfile = new FileStream("finalfile.txt", FileMode.Append, FileAccess.Write); ...
0
by: deloford | last post by:
Hi This is going to be a question for anyone who is an expert in C# Text Encoding. My situation is this: I have a Sybase database which is firing back ISO-8559 encoded strings. I am unable to...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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: 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: 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
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
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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...

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.