Replace utf-16 encoding with utf-8 encoding

   
 
#region License and Copyright
/*
 * Dotnet Commons Xml
 *
 *
 * This library is free software; you can redistribute it and/or modify it 
 * under the terms of the GNU Lesser General Public License as published by 
 * the Free Software Foundation; either version 2.1 of the License, or 
 * (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful, but 
 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY 
 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License 
 * for more details. 
 *
 * You should have received a copy of the GNU Lesser General Public License 
 * along with this library; if not, write to the 
 * Free Software Foundation, Inc., 
 * 59 Temple Place, 
 * Suite 330, 
 * Boston, 
 * MA 02111-1307 
 * USA 
 * 
 */
#endregion

using System;
using System.Collections;
using System.Collections.Specialized;
using System.Diagnostics;
using System.IO;
using System.Reflection;
using System.Text;
using System.Text.RegularExpressions;
using System.Xml;
using System.Xml.Xsl;
using System.Xml.Serialization;

//using Dotnet.Commons.Reflection;

namespace Dotnet.Commons.Xml
{
  
  ///  
  /// <summary>
  /// This utility class contains wrapper functions that help to ease the handling and 
    /// manipulation of Xml documents, such as adding an element, adding an attribute
    /// to an element, copying and cloning of nodes, etc.
  ///
  /// </summary>
  /// 
    
  public abstract class XmlUtils
  {
        /// <summary>
        /// Replace utf-16 encoding with utf-8 encoding
        /// </summary>
        /// <param name="sXml"></param>
        /// <returns></returns>
        public static string ReplaceUtf16toUtf8(string sXml)
        {
            return sXml.Replace(@"encoding=""utf-16""?>", @"encoding=""utf-8""?>");
        }
   }
}

   
     


Write the UTF-8 and ASCII encoded byte arrays


   
 

using System;
using System.IO;
using System.Text;

class Test
{
    public static void Main() 
    {        
        using (StreamWriter output = new StreamWriter("practice.txt")) 
        {
            // Create and write a string containing the symbol for Pi.
            string srcString = "Area = u03A0r^2";

            // Convert the UTF-16 encoded source string to UTF-8 and ASCII.
            byte[] utf8String = Encoding.UTF8.GetBytes(srcString);
            byte[] asciiString = Encoding.ASCII.GetBytes(srcString);
            
            // Write the UTF-8 and ASCII encoded byte arrays. 
            output.WriteLine("UTF-8  Bytes: {0}", BitConverter.ToString(utf8String));
            output.WriteLine("ASCII  Bytes: {0}", BitConverter.ToString(asciiString));
            
            Console.WriteLine(BitConverter.ToString(utf8String));
            Console.WriteLine(BitConverter.ToString(asciiString));
        }
    }
}



           
         
     


Write the UTF-16 encoded bytes of the source string


   
 
using System;
using System.IO;
using System.Text;

class Test
{
    public static void Main() 
    {        
        using (StreamWriter output = new StreamWriter("practice.txt")) 
        {
            // Create and write a string containing the symbol for Pi.
            string srcString = "Area = u03A0r^2";

            // Write the UTF-16 encoded bytes of the source string.
            byte[] utf16String = Encoding.Unicode.GetBytes(srcString);
            output.WriteLine("UTF-16 Bytes: {0}", BitConverter.ToString(utf16String));
            Console.WriteLine(BitConverter.ToString(utf16String));
        }
    }
}




           
         
     


Trace.WriteLine

using System;
using System.Data;
using System.Data.SqlClient;
using System.Threading;
using System.Diagnostics;
class MainClass {

static void Main() {
Trace.WriteLine(“Entered Main()”);

for (int i = 0; i < 6; i++) Trace.WriteLine(i); Trace.WriteLine("Exiting from Main()"); } } [/csharp]

Use Trace.Fail to alert a fail

   
 


using System;
using System.Data;
using System.Data.SqlClient;
using System.Threading;
using System.Diagnostics;


class Class1 {
    [STAThread]
    static void Main(string[] args) {
        SqlConnection dbConn = new SqlConnection("server=.;database=pubs;uid=sa;pwd=");

        SqlCommand dbComm = new SqlCommand("SELECT * FROM " + "authors", dbConn);
        SqlDataReader dr = null;
        Trace.WriteLine(DateTime.Now + " - Executing SQL statement");
        try {
            dbConn.Open();
            Trace.Assert(dbConn.State == ConnectionState.Open,"Error", "Connection failed...");
            dr = dbComm.ExecuteReader(CommandBehavior.CloseConnection);
            Trace.Assert(dr != null, "Error","The SqlDataReader is null!");

            while (dr.Read()) {
            }
        } catch {
            Trace.Fail("An error occurred in database access");
        } finally {
            if ((dr.IsClosed == false) &amp;&amp; (dr != null))
                dr.Close();
        }
    }
}

    


Week refers to the week of the month, with “5” meaning the last week.

   
 

using System;
using System.Globalization;
public class MainClass {
    public static void Main() {
        TimeZoneInfo wa = TimeZoneInfo.FindSystemTimeZoneById("W. Australia Standard Time");

        foreach (TimeZoneInfo.AdjustmentRule rule in wa.GetAdjustmentRules()) {
            Console.WriteLine("Rule: applies from " + rule.DateStart +
                                              " to " + rule.DateEnd);

            Console.WriteLine("   Delta: " + rule.DaylightDelta);

            Console.WriteLine("   Start: " + FormatTransitionTime
                                             (rule.DaylightTransitionStart, false));

            Console.WriteLine("   End:   " + FormatTransitionTime
                                             (rule.DaylightTransitionEnd, true));
            Console.WriteLine();
        }
    }
    static string FormatTransitionTime(TimeZoneInfo.TransitionTime tt,
                                    bool endTime) {
        if (endTime &amp;&amp; tt.IsFixedDateRule
                    &amp;&amp; tt.Day == 1 &amp;&amp; tt.Month == 1
                    &amp;&amp; tt.TimeOfDay == DateTime.MinValue)
            return "-";

        string s;
        if (tt.IsFixedDateRule)
            s = tt.Day.ToString();
        else
            s = "The first second third fourth last".Split()[tt.Week - 1] +
                " " + tt.DayOfWeek + " in";

        return s + " " + DateTimeFormatInfo.CurrentInfo.MonthNames[tt.Month - 1]
                 + " at " + tt.TimeOfDay.TimeOfDay;
    }
}