this was a post in a forum and the replynis also included - reply by Jon skeet [MVP].
********
how do you set the encoding format of an XML string? When Iwas outputting the XML to a file you can specify the encoding format likeso:
Post Question:
===========
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?
Reply:
=====
Use something like this:
public class StringWriterWithEncoding : StringWriter
{
Encoding encoding;
public StringWriterWithEncoding (Encoding encoding)
{
this.encoding = encoding;
}
public override Encoding Encoding
{
get
{
return encoding;
}
}
}
Anto Rocked!
13 years ago
No comments:
Post a Comment