Write XML in Base64 encoding

前端 未结 4 2025
后悔当初
后悔当初 2021-01-20 00:37

Following my questions about storing data, it has been suggested that I could use XML but then obfuscate the file by encoding it using Base64. I like this idea, and I have a

4条回答
  •  鱼传尺愫
    2021-01-20 00:56

    Write it:

    var sb = new StringBuilder(); 
    var settings = new XmlWriterSettings();
    //..settings
    using (var writer = XmlWriter.Create(sb, settings))
    {
        //...
    }
    //http://stackoverflow.com/questions/1564718/using-stringwriter-for-xml-serialization
    var encoding = new UnicodeEncoding(); 
    string s = Convert.ToBase64String(encoding.GetBytes(sb.ToString()));
    
    File.WriteAllText("c:\temp.txt", s);
    

    Read it:

    string readText = File.ReadAllText("c:\temp.txt");
    byte[] toDecodeByte = Convert.FromBase64String(readText);
    
    using (var stream = new MemoryStream(toDecodeByte))
    {
        using (XmlReader reader = XmlReader.Create(stream))
        {
            //.. read your xml
        }
    }
    

提交回复
热议问题