Connecting Tech Pros Worldwide Help | Site Map

XML Encoding Question

Waldy
Guest
 
Posts: n/a
#1: Nov 16 '05
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?


Jon Skeet [C# MVP]
Guest
 
Posts: n/a
#2: Nov 16 '05

re: XML Encoding Question


Waldy <waldy@notmail.com> wrote:[color=blue]
> 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.[/color]

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 - <skeet@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Waldy
Guest
 
Posts: n/a
#3: Nov 16 '05

re: XML Encoding Question


That's it! Thanks Jon.


Larry Richardson
Guest
 
Posts: n/a
#4: Nov 16 '05

re: XML Encoding Question


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!
Waldy
Guest
 
Posts: n/a
#5: Nov 16 '05

re: XML Encoding Question



"Jon Skeet [C# MVP]" <skeet@pobox.com> wrote in message
news:MPG.1bd06d1c4359ea2798b62d@msnews.microsoft.c om...[color=blue]
>
> Use something like this:
>
> public class StringWriterWithEncoding : StringWriter
> {
> Encoding encoding;
>
> public StringWriterWithEncoding (Encoding encoding)
> {
> this.encoding = encoding;
> }
>
> public override Encoding Encoding
> {
> get { return encoding; }
> }
> }
>[/color]

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.


Jon Skeet [C# MVP]
Guest
 
Posts: n/a
#6: Nov 16 '05

re: XML Encoding Question


Waldy <waldy@notmail.com> wrote:[color=blue][color=green]
> > Use something like this:
> >
> > public class StringWriterWithEncoding : StringWriter
> > {
> > Encoding encoding;
> >
> > public StringWriterWithEncoding (Encoding encoding)
> > {
> > this.encoding = encoding;
> > }
> >
> > public override Encoding Encoding
> > {
> > get { return encoding; }
> > }
> > }
> >[/color]
>
> 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.[/color]

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 - <skeet@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Closed Thread


Similar C# / C Sharp bytes