Converting Stream to String and back…what are we missing?
string test = "Testing 1-2-3";
// convert string to stream
byte[] byteArray = Encoding.ASCII.GetBytes(test);
MemoryStream stream = new MemoryStream(byteArray);
// convert stream to string
StreamReader reader = new StreamReader(stream);
string text = reader.ReadToEnd();
var stream = new MemoryStream();
var streamWriter = new StreamWriter(stream, System.Text.Encoding.UTF8);
Serializer.Serialize<SuperExample>(streamWriter, test);
public static string Serialize<T>(T value) {
if(value == null) {
return null;
}
XmlSerializer serializer = new XmlSerializer(typeof(T));
XmlWriterSettings settings = new XmlWriterSettings();
settings.Encoding = new UnicodeEncoding(false, false); // no BOM in a .NET string
settings.Indent = false;
settings.OmitXmlDeclaration = false;
using(StringWriter textWriter = new StringWriter()) {
using(XmlWriter xmlWriter = XmlWriter.Create(textWriter, settings)) {
serializer.Serialize(xmlWriter, value);
}
return textWriter.ToString();
}
}
public static T Deserialize<T>(string xml) {
if(string.IsNullOrEmpty(xml)) {
return default(T);
}
XmlSerializer serializer = new XmlSerializer(typeof(T));
XmlReaderSettings settings = new XmlReaderSettings();
// No settings need modifying here
using(StringReader textReader = new StringReader(xml)) {
using(XmlReader xmlReader = XmlReader.Create(textReader, settings)) {
return (T) serializer.Deserialize(xmlReader);
}
}
}
Converting Stream to String and back…what are we missing?的更多相关文章
- Stream To String , String To Stream
public static string StreamToString(Stream stream) { stream.Position = 0; using (StreamReader stremR ...
- string 转stream和stream转string
string test = “Testing 1-2-3″; // convert string to stream MemoryStream stream = new MemoryStream(); ...
- 获取报告 Stream转string,利用字符串分割转换成DataTable
protected void Button1_Click(object sender, EventArgs e) { MemoryStream stream = new MemoryStream(); ...
- dotnet中Stream、string及byte[]的相关操作
string与byte[](UTF-8) //string to byte[] string str = "abc中文"; //0x61 0x62 0x63 0xE4 0xB8 0 ...
- C++ code:string stream(string流)
如果有一个文件aaa.txt,有若干行,不知道每行中含有几个整数,要编程输出每行的整数之和,该如何实现? 由于cin>>不能辨别空格与回车的差异,因此只能用getline的方式逐行读入数据 ...
- 将String转化成Stream,将Stream转换成String
using System;using System.IO;using System.Text;namespace CSharpConvertString2Stream{ class Progr ...
- BufferHelp byte[] Stream string FileStream Image Bitmap
/******* * *** ***** ** ** * * * * * * * * ***** * * * * * * * * * * * * * * * ******* *** * ***** * ...
- c#中字节数组byte[]、图片image、流stream,字符串string、内存流MemoryStream、文件file,之间的转换
字节数组byte[]与图片image之间的转化 字节数组转换成图片 public static Image byte2img(byte[] buffer) { MemoryStream ms = ne ...
- C#图像处理:Stream 与 byte[] 相互转换,byte[]与string,Stream 与 File 相互转换等
C# Stream 和 byte[] 之间的转换 一. 二进制转换成图片 MemoryStream ms = new MemoryStream(bytes); ms.Position = 0; Ima ...
随机推荐
- [Effective JavaScript 笔记]第56条:避免不必要的状态
API有时被归为两类:有状态的和无状态的.无状态的API提供的函数或方法的行为只取决于输入,而与程序的状态改变无关.字符串的方法是无状态的.字符串的内容不能被修改,方法只取决于字符串的内容及传递给方法 ...
- CoreLoation
- (CLLocationManager *)locationManager { if (!_locationManager) { _locationManager = [[CLLocationMan ...
- lvs之nat技术的学习与实践
lvs nat 服务器搭建 1.配置三个虚拟机.一台用于做lvs 两台用于做web server 进行测试 (lvs服务器要配备两块网卡); lvs 服务器 两块网卡 分别为vmnet1 vm ...
- OpenResty(Nginx)+Lua+GraphicsMagick实现缩略图功能
http://www.hopesoft.org/blog/?p=1188 http://www.imagemagick.org/download/ 2.用法 原始图片是input.jpg,尺寸:160 ...
- Python中请使用isinstance()判断变量类型
一.isinstance() 在Python中可以使用type()与isinstance()这两个函数判断对象类型,而isinstance()函数的使用上比type更加方便. # coding=utf ...
- 【leetcode】Combination Sum
Combination Sum Given a set of candidate numbers (C) and a target number (T), find all unique combin ...
- com.sun.org.apache.xerces.internal.impl.io.MalformedByteSequenceException: 3 字节的 UTF-8 序列的字节 3 无效。
org.springframework.beans.factory.BeanDefinitionStoreException: IOException parsing XML document fro ...
- Python多进程(1)——subprocess与Popen()
Python多进程方面涉及的模块主要包括: subprocess:可以在当前程序中执行其他程序或命令: mmap:提供一种基于内存的进程间通信机制: multiprocessing:提供支持多处理器技 ...
- Simple Chroma Key 0.1.16 图片抠像(vs2003) 无任何插件
学习扣像的最基础代码 (只支持 BMP TGA) simplechromakey.rar
- dbVisualizer破解
下载dbvis.puk,替换C:\Program Files\DbVisualizer\lib\dbvis.jar中的文件. 替换后打开选手动的key:下载地址dbvis.license