C# Appending to a File

As I have been mainly developing for the web … it’s easy to loose track of the simple things like this:

    string path = @"c:\temp\MyTest.txt";

    // This text is added only once to the file.
    if (!File.Exists(path)) 
    {
        // Create a file to write to.
        using (StreamWriter sw = File.CreateText(path)) 
        {
            sw.WriteLine("Hello");
            sw.WriteLine("And");
            sw.WriteLine("Welcome");
        }	
    }

    // This text is always added, making the file longer over time
    // if it is not deleted.
    using (StreamWriter sw = File.AppendText(path)) 
    {
        sw.WriteLine("This");
        sw.WriteLine("is Extra");
        sw.WriteLine("Text");
    }	

    // Open the file to read from.
    using (StreamReader sr = File.OpenText(path)) 
    {
        string s = "";
        while ((s = sr.ReadLine()) != null) 
        {
            Console.WriteLine(s);
        }
    }

Querying XML in SQL

This is one of the most useful (yet underused) data query techniques I have come across whilst using SQL server

Storing application objects as XML in a SQL Server database is one of the most flexible ways of logging/archiving data used in web processes, incoming interface jobs, and populated schemas for printing/invoicing etc; and when there are no restriction on table sizes, this method of storage and retrieval is a god send

The underlying reason for this post is the fact that I rarely have to use these queries anymore as I am only dealing with logging or archiving with XML and tend to forget the syntax! Continue reading