Pages

Showing posts with label how to. Show all posts
Showing posts with label how to. Show all posts

Wednesday, January 4, 2012

Converting a DataSet to XML node excluding Schema

In this blog post I'm going to explain you on how to return an object of type "DataSet" from a web service (SOAP) method.
 
In general when we return any type from a web method, it returns XML formatted string which then need to be parsed to get the resultant output. Whether it is a simple or any complex object type like Collections or generic list


However, when we returning a DataSet the resultant output will include table data along with the Schema.

So, if we need data with schema then it is well & good. 
What can we do to get only data and eliminate the schema from the DataSet?

The following code explains how we can achieve this:

DataSet ds = //... Use your logic to get DataSet from some DB/source
string xmlContent = ds.GetXml(); 
System.Xml.XmlDocument doc = new System.Xml.XmlDocument();
doc.LoadXml(xmlContent);
System.Xml.XmlNode newNode = doc.DocumentElement;
return newNode;

GetXml method will return all the xml code in string variable for the corresponding DataSet (including schema).
Creating an XmlDocument object and load the Xml string using LoadXml method
Then we need to return DocumentElement of XmlDocument class to get only data excluding schema.

There is another technique to do the same with less effort, using Linq. Although, I never tried this to confirm..
//// When Using LINQ
//XElement newNode = XDocument.Parse(xmlContent).Root;

Try this and let me know if there are any other ways...

Monday, June 20, 2011

Generate Thumbnail Image

As part of our developer career we came across displaying user’s uploaded image onto the web page. This is the most common requirement when working for a photo manager site like Picasa or SnapFish or social networking domain.

Displaying image on a web page is not a big issue. However when considering the page performance we need to count if there are too many images within the page and most importantly the size of the image (good when in KB and worst in MB)

The worst scenario is when the end user uploads a photo which is of very high resolution say approximately ~2 - 4 MB in size.

To improve the page performance we either need to restrict user not to upload high resolution images or handle user’s images to render out thumbnail images which eventually occupy less weight on the page.

We have a technique in Asp.net to generate thumbnail images. We generally use "GetThumbnailImage" method of Bitmap class to generate thumbnail image of any size.

The following code snippet will generate thumbnail image as response header provided original image's binary stream and expecting thumbnail image width and height:

private void GetThumbnailImage(byte[] objByteStream, int intWidth, int intHeight)
{
byte[] image = (byte[])objByteStream;
System.Drawing.Bitmap uploadedimage = new System.Drawing.Bitmap(convertByteArrayToStream(image));
System.Drawing.Bitmap b = resizeImage(uploadedimage, intWidth, intHeight);
System.Drawing.Image.GetThumbnailImageAbort dummyCallBack = new System.Drawing.Image.GetThumbnailImageAbort(ThumbnailCallback);
System.Drawing.Image thumbNailImg = uploadedimage.GetThumbnailImage(b.Width, b.Height, dummyCallBack, IntPtr.Zero);
MemoryStream mstream = new MemoryStream();
thumbNailImg.Save(mstream, ImageFormat.Jpeg);
byte[] image2 = mstream.ToArray();
HttpContext.Current.Response.ContentType = "image/jpeg";
HttpContext.Current.Response.BinaryWrite(image2);
}

All we need is to create a handler class (.ashx) to render the newly generated thumnbail image back to the page.