Monday, August 29, 2016

How to download HTML page through C# console application?

Hello friends, in this article I will show that how to retrieve web page or download web page source to do so one should have basic knowledge of visual studio and c# programming language.

Here is the one line that will help to retrieve webpage with C#

Use System.Net.WebClient class to use below code.


System.Console.WriteLine(new System.Net.WebClient().DownloadString(url));


How to download web page with the help c# console application.


using System.Net;
//...
using (WebClient client = new WebClient ()) // WebClient class inherits IDisposable
{

    client.DownloadFile("http://yoursite.com/page.html", @"C:\localfile.html");

    // Or you can get the file content without saving it:

    string htmlCode = client.DownloadString("http://yoursite.com/page.html");
    //...

}



There are two ways directly save it to localfile.html or you can get the code and get it into the string variable.

Monday, August 22, 2016

What is FOR XML in SQL Server and how to use it?

What is for xml?

If we use SELECT query it returns as a rowset but there it is optional to retrieve formal results of SQL query as XML by specifying FOR XML. FOR XML is used in top level queries. In a FOR XML clause, you specify one of these modes:

  • RAW
  • AUTO
  • EXPLICIT
  • PATH
You can include a FOR XML clause only in SELECT statements, if those statements define the outer, or top-level, query. However, you can also include the clause in INSERT, UPDATE, and DELETE statements that are part of a sub-query.

How to use for xml?


[ FOR { BROWSE | <XML> } ] 
<XML> ::= 
XML  
    {  
      { RAW [ ('ElementName') ] | AUTO }  
        [  
           <CommonDirectives>  
           [ , { XMLDATA | XMLSCHEMA [ ('TargetNameSpaceURI') ]} ]  
           [ , ELEMENTS [ XSINIL | ABSENT ]  
        ] 

      | EXPLICIT  
        [  
           <CommonDirectives>  
           [ , XMLDATA ]  
        ] 

      | PATH [ ('ElementName') ]  
        [  
           <CommonDirectives>  
           [ , ELEMENTS [ XSINIL | ABSENT ] ] 
        ] 
     }  
 
 <CommonDirectives> ::=  
   [ , BINARY BASE64 ] 
   [ , TYPE ] 
   [ , ROOT [ ('RootName') ] ] 


RAW Mode

The RAW mode generates a single XML element for each row in the result set returned by the



SELECT e.EmployeeID, c.FirstName, c.MiddleName, c.LastName
FROM HumanResources.Employee e INNER JOIN Person.Contact c
   ON c.ContactID = e.ContactID
WHERE c.FirstName = 'XYZ'
FOR XML RAW;


Output:


<row EmployeeID="123" FirstName="XYZ" LastName="XYZ" />
<row EmployeeID="125" FirstName="XYZ" MiddleName="AA" LastName="ABC" />


Now in output we can see there “row” we can rename it by adding ('Employee') after RAW in the query

The AUTO Mode


The AUTO mode in a FOR XML clause is slightly different from the RAW mode in the way that it generates the XML result set. The AUTOmode generates the XML by using heuristics based on how the SELECT statement is defined. The best way to understand how this works is to look at an example. The following SELECT statement, as in the previous examples



SELECT Employee.EmployeeID, ContactInfo.FirstName,
ContactInfo.MiddleName, ContactInfo.LastName
FROM HumanResources.Employee AS Employee
INNER JOIN Person.Contact AS ContactInfo
ON ContactInfo.ContactID = Employee.ContactID
WHERE ContactInfo.FirstName = 'XYZ'
FOR XML AUTO, ROOT ('Employees');



Output:


</Employees>
    <Employees>
        <Employee EmployeeID="123">
        <ContactInfo FirstName="XYZ" LastName="XYZ" />
    </Employee>
    <Employee EmployeeID="125">
        <ContactInfo FirstName="XYZ" MiddleName="AA" LastName="ABC" />
    </Employee>
</Employees>




The EXPLICIT Mode

The EXPLICIT mode provides very specific control over your XML, but this mode is much more complex to use than the RAW or AUTOmodes. To use this mode, you must build your SELECT statements in such as way as to define the XML hierarchy and structure. In addition, you must create a SELECT statement for each level of that hierarchy and use UNION ALL clauses to join those statements.


SELECT 1 AS Tag,
NULL AS Parent,
e.EmployeeID AS [Employee!1!EmployeeID],
NULL AS [ContactInfo!2!FirstName!ELEMENT],
NULL AS [ContactInfo!2!MiddleName!ELEMENT],
NULL AS [ContactInfo!2!LastName!ELEMENT]
FROM HumanResources.Employee e INNER JOIN Person.Contact c
ON c.ContactID = e.ContactID
WHERE c.FirstName = 'XYZ'
UNION ALL
SELECT 2 AS Tag,
1 AS Parent,
e.EmployeeID,
c.FirstName,
c.MiddleName,
c.LastName
FROM HumanResources.Employee e INNER JOIN Person.Contact c
ON e.ContactID = c.ContactID
WHERE c.FirstName = 'XYZ'
ORDER BY [Employee!1!EmployeeID], [ContactInfo!2!FirstName!ELEMENT]
FOR XML EXPLICIT;



Output:


<Employee EmployeeID="123">
    <ContactInfo>
        <FirstName>XYZ</FirstName>
        <LastName>XYZ</LastName>
    </ContactInfo>
</Employee>
<Employee EmployeeID="125">
    <ContactInfo>
        <FirstName>XYZ</FirstName>
        <MiddleName>AA</MiddleName>
        <LastName>ABC</LastName>
    </ContactInfo>
</Employee>
 


The PATH Mode

When you specify the PATH mode in the FOR XML clause, column names (or their aliases) are treated as XPath expressions that determine how the data values will be mapped to the XML result set. By default, XML elements are defined based on column names. You can modify the default behavior by using the at (@) symbol to define attributes or the forward slash (/) to define the hierarchy. Let’s take a look at a few examples to demonstrate how all this works.


SELECT e.EmployeeID, c.FirstName,
c.MiddleName, c.LastName
FROM HumanResources.Employee AS e
INNER JOIN Person.Contact AS c
ON c.ContactID = e.ContactID
WHERE c.FirstName = 'XYZ'
FOR XML PATH ('Employee'), ROOT ('Employees
');



<Employees>
    <Employee>
        <EmployeeID>123</EmployeeID>
        <FirstName>XYZ</FirstName>
        <LastName>XYZ</LastName>
    </Employee>
    <Employee>
        <EmployeeID>125</EmployeeID>
        <FirstName>XYZ</FirstName>
        <MiddleName>AA</MiddleName>
        <LastName>ABC</LastName>
     </Employee>
</Employees>




Monday, August 15, 2016

How to create simple Android App?

What is Android?

Android is an open source and Linux-based Operating System for mobile devices such as smartphones and tablet computers. Android was developed by the Open Handset Alliance, led by Google, and other companies.

Android offers a unified approach to application development for mobile devices which means developers need only develop for Android, and their applications should be able to run on different devices powered by Android.

The source code for Android is available under free and open source software licenses. Google publishes most of the code under the Apache License version 2.0 and the rest, Linux kernel changes, under the GNU General Public License version 2.

Why Android ?

  • Open source
  • Larger developer and community reach
  • Increased marketing
  • Inter app integration
  • Reduce cost of development
  • Higher success ratio
  • Rich development envirnment

How to built android app?

Tools required for building app:
  • Java JDK5 or later version
  • Android SDK
  • Java Runtime Environment (JRE) 6
  • Android Studio
Create Android Application

The first step is to create a simple Android Application using Android Studio. Follow the option File -> New project ->Configure your new project -> selct factor your application is run on -> add activity ->Customise your activity -> and finally select finish wizard from the wizard list. Now name your application as HelloWorld using the wizard window as follows:


SELECT START A NEW APPLICATION PROJECT


ENTER AN APPLICATION NAME




SELECT MIN-SDK VERSION

If all things goes right following screen will appear





Before moving further there few directory to keep in mind

build: This contains the auto generated file which are as Aidl,Build configuration, and R(R.JAVA)

Libs: This is a directory to add the libraries to develop the android applications

src: This contains the .java source files for your project. By default, it includes an MainActivity.java source file having an activity class that runs when your app is launched using the app icon.

res: This is a directory,which is having drawable,layout,values,and android manifest file

res/layout: This is a directory for files that define your app's user interface.

AndroidManifest.xml: This is the manifest file which describes the fundamental characteristics of the app and defines each of its components

Main Activity file

This is default code generated by application.


package com.example.helloworld;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.MenuItem;
import android.support.v4.app.NavUtils;

public class MainActivity extends Activity {
   @Override
   public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);
   }
  
   @Override
   public boolean onCreateOptionsMenu(Menu menu) {
      getMenuInflater().inflate(R.menu.activity_main, menu);
      return true;
   }
}



Manifest File


<manifest xmlns:android="http://schemas.android.com/apk/res/android"
   package="com.example.helloworld"
   android:versionCode="1"
   android:versionName="1.0" >
  
   <uses-sdk
      android:minSdkVersion="8"
      android:targetSdkVersion="22" />
  
   <application
      android:icon="@drawable/ic_launcher"
      android:label="@string/app_name"
      android:theme="@style/AppTheme" >
      <activity
         android:name=".MainActivity"
         android:label="@string/title_activity_main" >
     
         <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER"/>
         </intent-filter>
     
      </activity>
     
   </application>
</manifest>



String file

As default this file will look like this


<resources>
   <string name="app_name">HelloWorld</string>
   <string name="hello_world">Hello world!</string>
   <string name="menu_settings">Settings</string>
   <string name="title_activity_main">MainActivity</string>
</resources>


Layout file

By default this file will look like this

This is an example of simple RelativeLayout which we will study in a separate chapter. The TextView is an Android control used to build the GUI and it have various attributes like android:layout_width, android:layout_height etc which are being used to set its width and height etc. The @string refers to the strings.xml file located in the res/values folder. Hence, @string/hello_world refers to the hello string defined in the strings.xml fi le, which is "Hello World!”.



<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
   xmlns:tools="http://schemas.android.com/tools"
   android:layout_width="match_parent"
   android:layout_height="match_parent" >

   <TextView
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_centerHorizontal="true"
      android:layout_centerVertical="true"
      android:padding="@dimen/padding_medium"
      android:text="@string/hello_world"
      tools:context=".MainActivity" />

</RelativeLayout>


Other interesting things?

Features of Android

Feature
Description
Beautiful UI
Android OS basic screen provides a beautiful and intuitive user interface.
Storage
SQLite, a lightweight relational database, is used for data storage purposes
Media support
H.263, H.264, MPEG-4 SP, AMR, AMR-WB, AAC, HE-AAC, AAC 5.1, MP3, MIDI, Ogg Vorbis, WAV, JPEG, PNG, GIF, and BMP
Web browser
Based on the open-source WebKit layout engine, coupled with Chrome's V8 JavaScript engine supporting HTML5 and CSS3.
Multi-tasking
User can jump from one task to another and same time various application can run simultaneously.
GCM
Google Cloud Messaging (GCM) is a service that lets developers send short message data to their users on Android devices, without needing a proprietary sync solution.
Multi-Language
Supports single direction and bi-directional text.