• 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);

    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);
            }
        }
    }
  • 相关阅读:
    redis--pipelining管道
    插入排序
    选择排序
    冒泡排序
    网页表格导入导出Excel
    easyUI的datagrid表格的使用
    软件工程实践总结(个人)
    Beta答辩总结
    Beta冲刺7
    Beta冲刺6
  • 原文地址:https://www.cnblogs.com/xpvincent/p/3728029.html
Copyright © 2020-2023  润新知