How  and 65279 and Other Byte Order Marks (BOM) Can Mess Up Your XML

4 Comments »

When you download XML text from the Web, you may find “garbage characters” in the start of your XML string.  For example, I encountered this result when I downloaded an XML string using WebClient.DownloadString method:

<Root><Item>Hello, World</Item></Root>

What you are likely seeing is a Byte Order Mark (BOM), which is a Unicode character that indicates the endian-ness (byte order) of a text file or stream.  The BOM is optional and will appear at the start of the text stream, if at all.  The BOM may also indicate in which of the several Unicode representations the text is encoded.

Read the rest of this entry »

C# Convert Byte Array to String

2 Comments »

It’s easy to convert a byte array to a string.  For an ASCII string, use the Encoding.ASCII.GetString static method:

byte[] buffer = new byte[10];
// todo: populate the buffer with string data
string s = Encoding.ASCII.GetString( buffer );

Convert String to Byte Array

1 Comment »

It’s easy to convert a string to a byte array.  For an ASCII string, use the Encoding.ASCII.GetBytes static method:

string s = "Test String";
byte[] byteArray = Encoding.ASCII.GetBytes( s );

Read File into Byte Array

1 Comment »

It’s easy to read a file into a byte array.  Just use the File.ReadAllBytes static method.  This opens a binary file in read-only mode, reads the contents of the file into a byte array, and then closes the file.

string filePath = @"C:test.doc";
byte[] byteArray = File.ReadAllBytes( filePath );

Set InitialDirectory for FolderBrowserDialog

2 Comments »

The .NET FolderBrowserDialog class does not have an InitialDirectory property like the OpenFileDialog class.  But fortunately, it’s quite easy to set an initial folder in the FolderBrowserDialog:

FolderBrowserDialog dialog = new FolderBrowserDialog();
dialog.RootFolder = Environment.SpecialFolder.Desktop;
dialog.SelectedPath = @"C:Program Files";
if (dialog.ShowDialog() == DialogResult.OK)
{
    Console.WriteLine( dialog.SelectedPath );
}

Note that you can choose other RootFolder’s like MyComputer and MyDocuments.  The SelectedPath must be a child of the RootFolder for this to work.

Given the sample code above, the dialog may look like this:

Visual Studio 2010 and .NET Framework 4.0 Released Today

No Comments »

Microsoft is releasing Visual Studio 2010, .NET Framework 4.0, and Silverlight 4 at the Visual Studio Developer Conference in Las Vegas.  VS 2010 and .NET 4 are available today, and Silverlight 4 will be available to download later this week.

Read more at DevTopics >>