How To Add Or Change XML Encoding With .NET

Last time I wrote about comparing XML strings in .NET, and continuing that theme I’d like to discuss another handy XML trick.  As the last article pointed out, not all XML strings are created equal.  In fact, sometimes they are missing the necessary XML version and encoding information, like in the following sample XML:

<SampleXML>
    <SampleField1>Testing</SampleField1>
    <SampleField2 value="test value" />
    <SampleField3 type="int">456</SampleField3>
</SampleXML>

Notice anything missing?   Yes, the declaration information is not at the start of the XML string.  Declaration information like in the below example:

<?xml version="1.0" encoding="utf-8"?>

Continue reading

Executing Retry Logic in C#

Sometimes an exception isn’t just an exception. For example, SQL can have connection or timeout issues and file IO can have problems competing for file access. In these situations, sometimes it makes sense to try executing the code again rather than letting it completely error out. Below is some C# sample code for executing some simple retry logic.

for (int retryAttempt = 1; retryAttempt <= MAX_RETRY_ATTEMPTS; retryAttempt++)
{
     try
     {
          // Perform code execution here
          break;
     }
     catch (Exception ex)
     {
          // Perform any logging here          if (retryAttempt < MAX_RETRY_ATTEMPTS)
          {
               Thread.Sleep(1000); // Sleep 1 second before retrying
          }
          else
          {
               throw;
          }
     }
}

As you can see, it’s pretty simple.  Execute the actual code logic within the try block, and perform any necessary exception logging within the catch block.

Richard Franzen
Sr. Developer
ImageSource, Inc.

How to Convert Office File to PDF File Format in C#

How to Convert Office file to PDF in C#.

These codes are used for Microsoft Office products with the Save As PDF add-in installed.

Note that You will need to add a reference to Microsoft.Office.Interop.(word,excel, or powerpoint)

Continue reading

HashSet .Net 3.5

The C# HashSet<T> data structure was introduced in the .NET Framework 3.5.
It is a very powerful data structure with no duplicate values. It comes with a bunch of built-in set functions. It inherits the ICollection interface, so the elements inside a HashSet can only be accessed through an enumerator.

Continue reading

Using the ‘using’ keywork in C#

C# Super HeroIn the System.Data.SqlClient namespace, SqlConnection and SqlCommand are two examples of managed types that use unmanaged resources down in the COM layer of the run-time. Microsoft says that all of these types must implement the IDisposable interface.

Continue reading

Javascript escape() and C#

Recently, I needed to transmute a Web form into Windows form for a client. There was a subtle issue involving parameters to a SQL stored proc; the stored proc returned matches and near matches to the input values. The return values were missing special characters like ampersand (&).

I kept looking at the output routine trying to figure out where the data was getting cooked. I walked back up the execution chain, checking the inputs until I found a call to escape() in the JavaScript on the original web form.
The solution was to import the Microsoft.JScript assembly and call the Microsoft.JScript.GlobalObject.escape() method.

Another good day at ImageSource, Inc.