Posts tagged ‘xml’

Non compliant XML in Configuration file Base64

I had a need to store some configuration data that is non XML compliant in my .net configuration file. As the configuration file is XML file I settled on storing the data in Base64 and then converting it back after it’s read from the configuration file. First we take the string and convert it to bytes then we convert the bytes to Base64 string that will be written to the configuration file. Reverse when you want to read the string from the configuration file.

And now for the code, as a UnitTest

// C#
string original = “&at;=@|”=’_EMPTY_'”;
// base64 value = JmF0Oz1AfCcnPSdfRU1QVFlfJw==

// to bytes and back test
byte[] dBytes = new System.Text.UTF8Encoding().GetBytes( original );
System.Text.UTF8Encoding enc = new System.Text.UTF8Encoding();
string str = enc.GetString(dBytes);
// compare
Assert.AreEqual( original, str);

// This is the string to put in the configuration file now Base64 formatted
string base64String = Convert.ToBase64String( dBytes );
// config string base64 value=JmF0Oz1AfCcnPSdfRU1QVFlfJw==
Console.WriteLine( “config string base64 value=”+ base64String );

// Convert Base64 back to a normal string, this needs to be done after reading
// the base 64 string from the config file
byte[] nonBase64bytes = Convert.FromBase64String( base64String );
// string from bytes
string fromBytes = new System.Text.UTF8Encoding().GetString(nonBase64bytes);
// Make sure it still adds up with the original
Assert.AreEqual(original, fromBytes );

C# Xml database

I was thinking I needed a small footprint database the other day when coding a small app. If you need something to save small amount of data you can just serialize it to Xml file and back. There is your little database. Obviously not very usable for heavy load, but with a small amount of data it works just fine. There is not much involved lets take a look.

public class Car
    {
        public string Model = string.Empty;
        public int Year = 0;
        //
        public Car() {}
    }

        /// Reads car objects from xml file
        public static List<Car> readXML()
        {
            // Trap
            if (!File.Exists(XML_FILE)) return new List<Car>();

            // Deserialize, load from XML
            FileStream fs = new FileStream(XML_FILE, FileMode.Open);
            XmlReader reader = new XmlTextReader(fs);
            XmlSerializer ser = new XmlSerializer(typeof(List<Car>));
            List<Car> myList = (List<Car>)ser.Deserialize(reader);
            fs.Close();
            //
            return myList;
        }

        /// Writes car objects to xml file
        public static void writeXML(List<Car> plStocks)
        {
            // Now create the Xml
            XmlSerializer s = new XmlSerializer(typeof(List<Car>));
            TextWriter writer = new StreamWriter(XML_FILE);
            // Serializes the objects in list
            s.Serialize(writer, plStocks);
            // done, close the file
            writer.Close();
        }

Tweet trends

I just played with the Twitter api / links to get to know it.  Took their trend feed which is a json stream.  The stream includes names of the trends and it’s URL.   After the json info is imported the next thing is to hit the RSS / Xml feed for each topic.  Load it into entry objects including authors and spit out the ones that include links, up to the last three per category.  As that’s what gives you the best bang for the buck checking on Tweet trends.  You can take a look at it here.  The feed it self is live and updates real time, but you have to refresh the page to get the latest.  Thanks to @johnadepue for his input.

Then I went on Twitter Grader for fun.  It turns out I’m in negative territory.  My ranking shows up as “57,255 Overall rank, out of 56,047“  Althought I only been a few days on Twitter that doesn’t feel right.   I’m graded a bit less than the whole Twitter population.  How is that for a math problem.