XML Encoding Question

W

Waldy

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?
 
J

Jon Skeet [C# MVP]

Waldy said:
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; }
}
}
 
L

Larry Richardson

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 ?
 
W

Waldy

Jon Skeet said:
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.
 
J

Jon Skeet [C# MVP]

Waldy said:
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.
 

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