String to Stream C#

In many situation we find our self that we need to get a stream object from string.

In this case we can utilize Encoding Class and Memory Stream class to generate Stream from a String.

Encoding.GetByte method helps to generate byte array from given string.

var stream = new MemoryStream(Encoding.UTF8.GetBytes("some text goes here!.."))

Seems very small, but useful ūüôā

msbuild copy

Copy directory recursively task in MSBuild

So I ran into a situation where I needed to copy a directory to a specific destination through MSBuild.

This is how I could achieve my goal.

<Project xmlns="">
<CloudArtifacts Include=".Cloud\Data\**\*.*"/>
<Target Name="CopyFiles">
<Copy SourceFiles="@(CloudArtifacts)" DestinationFiles="@(CloudArtifacts->'c:\PackableFiles\%(RecursiveDir)%(Filename)%(Extension)')" />

We can get more details on MS Build Copy task in this link. 


Enabling Cross-Origin Requests in ASP.NET Web API

Response to preflight request doesn’t pass access control check: No ‘Access-Control-Allow-Origin’ header is present on the requested resource. Origin ‘<domain>’ is therefore not allowed access. The response had HTTP status code 405.


We want to expose our api to be called from other domain. In default browsers doesen’t allow cross domain xhr requests.

To get it fixed we want to allow CORS in our web api.

I have created video on enabling CORS in web api.

watch video

More about CORS in Web api

Reading Excel files easily in c# using Excel Data Reader

So I found a useful Package to read excel files easily. No more cumbersome codes needed.

I have written post on How to read Excel file in c# in here . But lot of codes.

So I found ExcelDataReader package in github.

Excel Data Reader

It is awesome.

Just few lines of codes to read files as below.

string filePath = "Book1.xlsx";
FileStream stream = File.Open(filePath, FileMode.Open, FileAccess.Read);

IExcelDataReader excelReader = ExcelReaderFactory.CreateOpenXmlReader(stream);

excelReader.IsFirstRowAsColumnNames = true;
DataSet result = excelReader.AsDataSet();



Download sample project file from github

Serializing Lists of Classes to XML

So some times we want to convert object to xml. We can follow severel cumbersome methods. So I am using Utlity methods to convert c# object to xml and xml to c# object.

Suppose we have class like below

public class Student
 public int Id { get; set; }
 public string FirstName { get; set; }
 public string LastName { get; set; }

And we want serialize List<Student> to xml. So we can use this utlity function

/// <summary>
/// Serialize object to xml
/// </summary>
/// <typeparam name="T">Object type</typeparam>
/// <param name="obj">Object</param>
/// <returns>XML</returns>
public static string ToXml<T>(T obj)
using (StringWriter stringWriter = new StringWriter(new StringBuilder(), CultureInfo.InvariantCulture))
XmlSerializer xmlSerializer = new XmlSerializer(typeof(T));
xmlSerializer.Serialize(stringWriter, obj);
return stringWriter.ToString();

As a example we populate List<student>  with dummy data

 List <Student> studentList = new List<Student>();

studentList.Add(new Student {
 FirstName = "jeevan",
 LastName = "JLast name",
 Id = 1

studentList.Add(new Student
 FirstName = "john",
 LastName = "johnLast name",
 Id = 2

Then call out utility function

string xml = Utility.ToXml(studentList);

then out xml variable look like as below

<?xml version="1.0" encoding="utf-16"?>
<ArrayOfStudent xmlns:xsi="" xmlns:xsd="">
<LastName>JLast name</LastName>
<LastName>johnLast name</LastName>


This configuration section cannot be used at this path. This happens when the section is locked at a parent level. Locking is either by default (overrideModeDefault=”Deny”), or set explicitly by a location tag with overrideMode=”Deny” or the legacy allowOverride=”false”.

So when I am hosting ASP.NET MVC web site in iis 7 I got following error.


So I followed following steps to get this solved.

Click “Start button”
in the search box, enter “Turn windows features on or off”
in the features window, Click: “Internet Information Services”
Click: “World Wide Web Services”
Click: “Application Development Features”
Check (enable) the features. I checked all but CGI.

Now it is solved. I think it might help you.

The remote server returned an error: (417) Expectation failed is solved

what is Expect100Continue Property

       According to 

The purpose of the 100 (Continue) status  is to allow a client that is sending a request message with a request body to determine if the origin server is willing to accept the request (based on the request headers) before the client sends the request body. In some cases, it might either be inappropriate or highly inefficient for the client to send the body if the server will reject the message without looking at the body.

more details about 100 continue property

I got this error when try to call web method in c#. So finally I salved this error by applying following things.

  1. Adding following statement right before calling web method

 System.Net.ServicePointManager.Expect100Continue = false.

2. Adding following section in web.config inside <configuration>

<servicePointManager expect100Continue=”false” />

more detail on MSDN