public void SetString(string encoding = null) { Encoding enc = TextEncodingEnum.GetEncodingByName(encoding, true); _stringWriter = new StringWriterWithEncoding(enc); _writer = new XmlTextWriter(_stringWriter); SetDefaultOptions(); }
public XmlWriterSettings GetClrSettings(bool addBom = false) { var result = new XmlWriterSettings { Indent = Indent, IndentChars = IndentChars, NewLineOnAttributes = IndentAttributes, Encoding = TextEncodingEnum.GetEncodingByName(Encoding, addBom), NewLineChars = System.Environment.NewLine, NamespaceHandling = NamespaceHandling.OmitDuplicates }; return(result); }
public void OpenFile(string path, string encoding = null, IValue addBOM = null) { Encoding enc; if (addBOM == null) { enc = TextEncodingEnum.GetEncodingByName(encoding, true); } else { enc = TextEncodingEnum.GetEncodingByName(encoding, addBOM.AsBoolean()); } _writer = new XmlTextWriter(path, enc); _stringWriter = null; SetDefaultOptions(); }
public void OpenFile(string fileName, string encoding = null, IValue addBOM = null, IValue settings = null) { if (IsOpen()) { Close(); } bool bAddBOM = false; if (addBOM != null) { bAddBOM = addBOM.AsBoolean(); } StreamWriter streamWriter; try { if (encoding != null) { streamWriter = Environment.FileOpener.OpenWriter(fileName, TextEncodingEnum.GetEncodingByName(encoding, bAddBOM)); } else { streamWriter = Environment.FileOpener.OpenWriter(fileName, TextEncodingEnum.GetEncodingByName("UTF-8", bAddBOM)); } } catch (Exception e) { throw new RuntimeException(e.Message, e); } _writer = new JsonTextWriter(streamWriter); if (settings == null) { SetDefaultOptions(); } else { SetOptions(settings); } SetNewLineChars(streamWriter); }
public string GetBodyAsString(IValue encoding = null) { // Формируем кодировку как в 1С. Если не указана, смотрим Content-Type. Если там не указана - используем UTF8 System.Text.Encoding enc = System.Text.Encoding.UTF8; if (encoding != null) { enc = TextEncodingEnum.GetEncoding(encoding); } else { System.Text.RegularExpressions.Regex regex = new System.Text.RegularExpressions.Regex("charset=([^\\\"']+)", System.Text.RegularExpressions.RegexOptions.IgnoreCase); string charsetString = regex.Match(_httpContext.Request.ContentType).Value; if (charsetString != "") { // Что-то нашли try { //charsetString.Substring(8) делает "charset=Кодировка" -> "Кодировка" enc = TextEncodingEnum.GetEncodingByName(charsetString.Substring(8)); } catch { // что то не так, осталась UTF8 } } } System.IO.Stream str = _httpContext.Request.InputStream; int bytes_count = Convert.ToInt32(str.Length); byte[] buffer = new byte[bytes_count]; str.Seek(0, System.IO.SeekOrigin.Begin); str.Read(buffer, 0, bytes_count); return(enc.GetString(buffer)); }