5

根据我关于存储数据的问题,有人建议我可以使用 XML,然后通过使用 Base64 对其进行编码来混淆文件。我喜欢这个想法,并且在XML中实现了我想要的,但是我不知道如何将它保存在Base64中。到目前为止,这是我的代码:

XmlWriterSettings settings = new XmlWriterSettings();
settings.Indent = true;

XmlWriter write = XmlWriter.Create("C:\\Users\\Andy\\Desktop\\database.xml", settings);
write.WriteStartDocument();
write.WriteStartElement("Database");
write.WriteStartElement("Entry");
write.WriteAttributeString("key", "value");
write.WriteEndElement();
write.WriteEndElement();

write.Flush();
write.Close();
4

4 回答 4

5

写下来:

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

阅读:

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
    }
}
于 2013-07-24T13:52:58.260 回答
3

您需要将您的 xmlwriter 转换为这样的字符串

 using (var sw = new StringWriter()) {
 using (var xw = XmlWriter.Create(sw)) {
// Build Xml with xw.


   }
  return sw.ToString();
}

然后将您的字符串转换为 Base64。并将其写入文件

于 2013-07-24T13:53:23.703 回答
1

这是一个使用MemoryStream

使用 MemoryStream 将 XML 数据保存到 base64 编码数据并将其写入文件。

using (var ms = new MemoryStream())
{   
    // Memory Stream
    using (XmlWriter writer = XmlWriter.Create(ms,settings)) 
    {
        writer.WriteStartDocument();
        writer.WriteStartElement("Database");
        writer.WriteStartElement("Entry");
        writer.WriteAttributeString("key", "value");
        writer.WriteEndElement();
        writer.WriteEndElement();

        writer.Flush();
        writer.Close();

        // XML Data (Debug)
        Console.WriteLine(new UTF8Encoding().GetString(ms.ToArray()));

        // Pre encoding hex (Debug)
        Console.WriteLine(BitConverter.ToString(ms.ToArray()));

        string s = Convert.ToBase64String(ms.ToArray());
        Console.WriteLine(s); // (Debug)

        // Post encoding
        File.WriteAllText(@"C:\Temp\A.enc", s);
    }
}

回读文件

// Read encoded file back to xml
string enc_text = File.ReadAllText(@"C:\Temp\A.enc");
Console.WriteLine(enc_text); // (Debug)

// Pre encoding hex
byte[] mem_bytes = Convert.FromBase64String(enc_text);
Console.WriteLine(BitConverter.ToString(mem_bytes)); // (Debug)

// XML Data
Console.WriteLine(new UTF8Encoding().GetString(mem_bytes));

// Read byte array into Reader
using (var stream = new MemoryStream(mem_bytes))
{
    using (XmlReader reader = XmlReader.Create(stream))
    {
        // Use reader as desired
    }
}
于 2013-07-24T14:57:45.510 回答
1

这将包含您正在寻找的信息:

http://msdn.microsoft.com/en-us/library/system.xml.xmlwriter.writebase64.aspx

高温下,

d3

于 2013-07-24T13:52:13.407 回答