Converting Stream to String and back…what are we missing?
2014-05-14 16:16
337 查看
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);
share|improve this answer
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); } } }
相关文章推荐
- How to run eclipse in clean mode? and what happens if we do so?
- 802.11 WDS how does it work, when to use it and what are the limitations
- Why I can't use non-string key in the myMap[myKey] expression? And what to do now?
- you are the ont that we would like to trust and ca...
- How to invoke the method of managed bean and render view in JSF when we are outside the lifecycle of JSF
- CVPR2012文章阅读(1)-What AreWe Looking For: Towards Statistical Modeling of Saccadic Eye Movements and Visual Saliency
- What are the most important data structure and algorithms to prepare for Google Interview?
- Four Logical Relationships of Project Management: What They Are and How To Use Them
- We are already in the process of making 11 connections and the number of simultaneous builds has been throttled to 10
- char* convert to string and back
- 8 Free Tools to Find What Files and Folders are Taking Up Hard Drive Space
- JWT vs JWS vs JWE - What They Are and When to Use Which
- WMS has encountered a problem and needs to close. We are sorry for the inconvenience.
- Convert IP addresses from a dots-and-number string to a struct in_addr and back
- string to stream and stream to string
- portable Executable: Converting RVA to File Offset and Back
- The expected argument types are (java.util.Map) but the supplied types were(java.lang.String) and converted to (null).
- Java - "==" and "equals" are different method to String Class
- Index and length must refer to a location within the string是什么错?
- 【摘自Devx】How to pass parameters to Thread in Windows Forms -- and get results back