XML – Rules of XML serialization

  1. XML Serialization serializes the public fields and properties of a class, or the parameters and return values of methods into an XML stream.
  2. XML Serialization does not include methods, indexers, private fields, or read-only properties (except read-only collections). Because XML is an open standard, the resulting XML stream can be processed by any application on any platform. For example: ASP.NET Web Services use XML Serialization to create XML streams to pass as data throughout the Internet or Intranets. Conversely, deserialization takes such streams and constructs an object.
  3. The following items can be serialized using XmlSerialzer:
    • Public read/write properties.
    • Public fields.
    • Classes that implement ICollection or IEnumerable.
    • XmlElement objects.
    • XmlNode objects.
    • DataSet objects.
  4. XmlSerialzer gives complete control over serializing an object into XML. For example, XmlSerialzer enables you to:
    • Specify whether a field or a property should be encoded as an element or as an attribute.
    • Specify which XML namespace to use.
    • Specify the name of an element or attribute if a field / property name is inappropriate.

XML Serialization Considerations

Following should be considered when using XmlSerialzer class:

  1. Type identity and assembly information is not included. In other words, XML serialization does not maintain type fidelity. To maintain type fidelity use binary serialization instead.
  2. Only public properties and fields can be serialized. To serialize non-public data use binary serialization instead.
  3. A class must have a default constructor to be serialized with the XmlSerialzer class.
  4. Methods cannot be serialized.
  5. XmlSerialzer class can serialize classes that implement IColleciton and IEnumerable differently if they meet certain requirements.
Share this:
Share

C# – Rules of Private Constructor

  1.  private Constructor is used when you don’t want to create an object for the class.These are commonly used in classes that contain static members only.If a class has one or more private constructors and no public constructors, then other classes (except nested classes) are not allowed to create instances of this class.
  2. We cannot inherit from classes that are having only private constructors. The reason is suppose if you are inheriting from a base class which has only private constructors and if you create an object for the derived class then the base class constructor will be called. Because the base class contains only private constructors and  due to ‘private’ access modifier  it is not accessible from the derived  class. So it will give you an error with a syntax as follows ‘Error 1 ’ConsoleApplication2.Program.Program()’ is inaccessible due to its protection level’
  3. Where do you find Private constructors are useful?

    Private constructors can also be useful for:  classes containing only static utility methods  classes containing only constants  type safe enumerations.

Share this:
Share

SQL SERVER – How to Remove All Characters/Numbers From a String Using T-SQL?

Few days back, my friends had asked a question “I want to get only integer part in string. for example, string contain ‘asb645pqr231means’ i want only integer part like 64523. how can i get?“. I have mentioned two method to do it.

Method 1:
SET NOCOUNT ON
DECLARE @loop INT
DECLARE @str VARCHAR (8000)
SELECT @str = ‘asb645pqr231’
SET @loop = 0
WHILE @loop < 26
BEGIN
SET @str = REPLACE (@str, CHAR (65 + @loop), ”)
SET @loop = @loop + 1
END
SELECT @str

Method 2:
SET NOCOUNT ON
DECLARE @KeepValues VARCHAR (50)
DECLARE @Str VARCHAR (500)
SET @Str= ‘asb645pqr231’
SET @KeepValues = ‘%[^a-z]%’ — if you want only characters in the output
SET @KeepValues = ‘%[^0-9]%’ — If you want only integers in the output
WHILE PATINDEX (@KeepValues, @Str) > 0
SET @Str = STUFF (@Str, PATINDEX(@KeepValues, @Str), 1, ”)
select @Str

There may be more ways do it.

Share this:
Share

SQL SERVER – How to do IF…THEN…ELSE in MS SQL SERVER?

In MS SQL Server, we can use a CASE statement to implement IF…THEN. Here is the example of the CASE statement.

If your logic is as follows:

IF 1 > -1
THEN
‘TRUE’
ELSE
‘FALSE’

You can use CASE statement as follows:

— SQL Server 2008 and earlier version solution
SELECT CASE WHEN -1 < 1 THEN ‘TRUE’ ELSE ‘FALSE’ END AS Result
GO
A New function “IIF” has introduced in MS SQL 2012. This function is shorthand way for writing CASE statement. These functions take three arguments. If the first argument is true, it will return the second argument as result or it will return the third argument as result.

— SQL Server 2012 solution

Example 1: IIF Usage
SELECT IIF ( -1 < 1, ‘TRUE’, ‘FALSE’ ) AS Result;
GO

Example 3: IIF with NULL
DECLARE @Variable INT=NULL
SELECT IIF ( @Variable IS NULL, ‘Value is Null’, ‘Value is NOT NULL’ ) AS Result;
GO

Example 2: Nested IIF
SELECT IIF ( 1 > -1, IIF ( 1 = 1, ‘Inner True’, ‘Inner False’ ), ‘FALSE’ ) AS Result;

In above example, we can see how IIF is used instead of CASE statement. IIF can be used the same way as CASE statement in SELECT statement.

Share this:
Share

SQL SERVER – Simple Example of WHILE Loop With CONTINUE and BREAK Keywords

I have tried to explain the usage of simple WHILE loop in first example. BREAK keyword will exit the stop the while loop and control is moved to next statement after the while loop. CONTINUE keyword skips all the statement after its execution and control is sent to first statement of while loop. Run following examples in Query Editor and see the result. This is very easy to understand example.

1. Example of WHILE Loop

DECLARE @intFlag INT
SET @intFlag = 1
WHILE (@intFlag <=5)
BEGIN
PRINT @intFlag
SET @intFlag = @intFlag + 1
END
GO

ResultSet:
1
2
3
4
5

2. Example of WHILE Loop with BREAK keyword

DECLARE @intFlag INT
SET @intFlag = 1
WHILE (@intFlag <=5)
BEGIN
PRINT @intFlag
SET @intFlag = @intFlag + 1
IF @intFlag = 4
BREAK;
END
GO

ResultSet:
1
2
3

3. Example of WHILE Loop with CONTINUE and BREAK keywords

DECLARE @intFlag INT
SET @intFlag = 1
WHILE (@intFlag <=5)
BEGIN
PRINT @intFlag
SET @intFlag = @intFlag + 1
CONTINUE;
IF @intFlag = 4 — This will never executed
BREAK;
END
GO

ResultSet:
1
2
3
4
5

Share this:
Share