示例#1
0
    public Encoding LookupEncoding(string tryEncoding) {
      Encoding encoding = null;

      if (tryEncoding == "windows-1252") {
        encoding = new Windows1252Encoding();
      } else {
        try {
          encoding = Encoding.GetEncoding(tryEncoding);
        } catch (ArgumentException) {
          // The user didn't present a valid string such as "utf-8".
          // We're supposed to do auto-encoding here, but I'm too lazy. Guess
          // at "utf-8". Don't throw an error.
          // http://www.w3.org/TR/FileAPI/#encoding-determination
          //
          // Silverlight actually only supports UTF-8 and UTF-16, so this
          // fallback will happen lots.
          // http://msdn.microsoft.com/en-us/library/system.text.encoding(v=vs.95).aspx
          encoding = Encoding.UTF8;
        }
      }

      return encoding;
    }
示例#2
0
        /// <summary>
        /// Return a <see cref="Encoding" /> object associated with the specified <see cref="Charset" />.
        /// The Encoding object is associated with custom implementations for the
        /// <see cref"EncoderFallback" /> and <see cref"DecoderFallback" /> objects so
        /// that it is possible to determine whether any replacement occurred during decoding.
        /// </summary>
        /// <param name="charset"></param>
        /// <returns></returns>
        public static Encoding GetEncoding(Charset charset)
        {
            var decoderFallback = new CustomReplacementDecoderFallback();
            var encoderFallback = EncoderFallback.ReplacementFallback;

            switch (charset)
            {
            case Charset.Greece:
            {
                var encoding = Windows1253Encoding.Create(
                    encoderFallback
                    , decoderFallback)
                ;
                return(encoding);
            }

            case Charset.CentralEasternEurope:
            {
                var encoding = Windows1250Encoding.Create(
                    encoderFallback
                    , decoderFallback)
                ;
                return(encoding);
            }

            case Charset.WesternEurope:
            {
                var encoding = Windows1252Encoding.Create(
                    encoderFallback
                    , decoderFallback)
                ;
                return(encoding);
            }

            case Charset.UnicodeBe:
            {
                var cp       = Encoding.BigEndianUnicode.CodePage;
                var encoding = Encoding.GetEncoding(cp
                                                    , EncoderFallback.ReplacementFallback
                                                    , decoderFallback
                                                    );
                return(encoding);
            }

            case Charset.UnicodeLe:
            {
                var cp       = Encoding.Unicode.CodePage;
                var encoding = Encoding.GetEncoding(cp
                                                    , EncoderFallback.ReplacementFallback
                                                    , decoderFallback
                                                    );
                return(encoding);
            }

            case Charset.UTF8:
            default:
            {
                var cp       = Encoding.UTF8.CodePage;
                var encoding = Encoding.GetEncoding(cp
                                                    , EncoderFallback.ReplacementFallback
                                                    , decoderFallback
                                                    );
                return(encoding);
            }
            }
        }