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