0

I have this code to keep some configuration data

XmlSerializer xmlSerializer = new XmlSerializer(typeof(Entities.Application));
TextReader textReader = new StreamReader(XMLFile);
sequences = (Entities.Application)xmlSerializer.Deserialize(textReader);
textReader.Close();

Now I want to hide it a little bit and somehow use a binary format over XML (to keep XML anyway).

Is it possible to do? (Or there are some others approaches as well?)

How do it can be done?

Thank you!

1
  • 1
    What do you mean by "binary format over xml?" Could you not just binary serialize instead, if you want the data to be unreadable? Commented Aug 1, 2012 at 14:27

2 Answers 2

1

If you want to obscure the XML, then two simple options are encoding the XML with Base64 or using Base64 in an XML element.

// Simple Base64 conversion (using UTF8 for simplicity)
// ========

// Assume [sequences] is variable of type [Entities.Application]
string xmlString;
var xs = new XmlSerializer(typeof(Entities.Application));
using (var sw = new StringWriter())
{
    xs.Serialize(sw, sequences);
    xmlString = sw.ToString();
}
string encoded = System.Convert.ToBase64String(
                    System.Text.Encoding.UTF8.GetBytes(xmlString));

// Converting encoded [Entities.Application] to decoded XML string,
// using some of your code for consistency.
string encoded;
using (TextReader textReader = new StreamReader(XMLFile))
{
    encoded = textReader.ReadToEnd();
    textReader.Close();
}
string decoded = System.Text.Encoding.UTF8.GetString(
                    System.Convert.FromBase64String(encoded));
XmlSerializer xmlSerializer = new XmlSerializer(typeof(Entities.Application));
using (var sr = new StringReader(decoded))
{
    sequences = (Entities.Application)xmlSerializer.Deserialize(sr);
}


// Inserting Base64 in XmlElement
// ========

// Optional: the old MSXML.DOMDocument had nodeTypedValue, and you
// can set the same attributes if you are going to use DOMDocument, although
// it is still a good idea to tag the element so you know its datatype.
node.SetAttribute("xmlns:dt", "urn:schemas-microsoft-com:datatypes");
node.SetAttribute("dt", "urn:schemas-microsoft-com:datatypes", "bin.base64");

// Assume serialized data has already been encoded as shown above
var elem = node.AppendChild(xmlDoc.CreateTextNode(encoded));
Sign up to request clarification or add additional context in comments.

Comments

1

You'll still have to encode your binary data as text. Is it already compressed? If so, that's probably the first step. Then you'll need to decide how you want to encode the binary. Base91 is meant to be pretty efficient: http://base91.sourceforge.net/

Comments

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.