5

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 achieved what I want in XML, but I don't know how to save it in Base64. This is my code so far:

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 Answers 4

6

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
    }
}
Sign up to request clarification or add additional context in comments.

26 Comments

Thank you for your answer. I would like to encode the entire XML file though, so could you please expand on that part.
+1 Thank you for the edit. So would I then just use a writer object to write s to a file? Also, I know I didn't ask it originally but could you show me how to read the XML file as well please?
Please do not use ASCIIEncoding. You should use UTF8Encoding.
@Andy: You CAN include non-ASCII characters in the XML, regardless of whether you use ASCII or UTF8 when writing the base64 string. As was pointed out, base64 encodes binary data using only ASCII characters--that's the whole point of it. When it's decoded, you get back the original binary data.
@Andy: Compression and/or any non-trivial encryption (anything beyond a simple substitution cipher) will be slower than base64 encoding. base64 encoding is a very simple transformation. It will take longer to write the encoded string to disk than it takes to encode the string.
|
3

You need to convert your xmlwriter to string like this

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


   }
  return sw.ToString();
}

then convert your string to Base64. and write it to file

4 Comments

Thank you for your answer. So I would I then have to create a new writer object for the Base64 encoded data?
+1. You should add the code that shows how to convert the returned string to base64 and write it to file.
@EhsanUllah Yes, please do :)
@EhsanUllah Yeah I have now thank you. It's ok, thank you for your help though.
1

This will have the information that you are looking for:

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

HTH,

d3

2 Comments

Thanks for your answer but I want to encode the entire XML file rather than just the values.
That's only good for writing specific binary data. That won't base64 encode the entire XML file.
1

Here is an example using a MemoryStream

Save the XML data using MemoryStream to base64 encode the data and write it to a file.

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 the file back

// 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
    }
}

3 Comments

Thank you very much for your answer. Regarding reading the file back, how could I actually get the data in the XmlReader rather than just print it? Which Stream object would be best to use?
@Andy - I updated to show how to create the XmlReader from the byte array data.
Thank you very much. I thought it would be something like that, but I'm very new to C#.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.