Wednesday, June 23, 2010

玉兰油illumiator

StringWriter and different Encodings

Recently I came across a small problem in assembling a slogan for school practical work: The idea was to build a helper for kids can easily download XML files.

The problem was that I was in charge of lowering the file stream, it touched up a bit and then went from a XDocument to a string using StringWriter , then writing the string, it was adding the XML header of encoding = "utf-16" . All this would not be a problem if it was not, the idea behind this was that just as they reached the string, you could save to disk with StreamWriter using the simpler constructors without having to specify encodings.

As StringWriter can not change the encoding you use, the solution was to create a subclass of StringWriter and do override Encoding property .

The class was so small:

 UTF8StringWriter 
class:
StringWriter {public override
System.Text.Encoding Encoding

{get {

System.Text.Encoding.UTF8 return;

}}}

I hope you find it useful! Zaiden


0 comments:

Post a Comment