Tuesday, October 15, 2019

XML Serialization - how to add namespace as prefix to XML Element or XML Attribute

XML Serialization Process converts .net object into XML format and by default object property or class name becomes XMLElement or root element until there is explicitly set ElementName or AttributeName property for XMLElement or XMLAttribute.
Here is an example without element Name or attribure Name

  public class Order
    {
        public int OrderNumber { get; set; }

        public string Item { get; set; }

        public string User { get; set; }

        [XmlAttribute]
        public string version { get; set; }
    }


XML String:

XML Serialization XML String

Now let specify element name and attribute name for each XML Element and XML Attribute tags

    public class Order
    {
        [XmlElement(ElementName = "OrderNum")]
        public int OrderNumber { get; set; }

        [XmlElement(ElementName = "ItemDetails")]
        public string Item { get; set; }

        [XmlElement(ElementName = "UserInfor")]
        public string User { get; set; }

        [XmlAttribute(AttributeName="OrderVersion")]
        public string version { get; set; }
    }

XML String:


XML Serialization XML String with XMLElement

Add Namespace as Prefix to XML element or attribute:

We will learn how to add namespace as prefix to XML element or attribute

·         First, we need to define XmlSerializerNamespaces class object, which hold reference of namespace and its prefix.
·         Second, we need to specify of namespace of XMLElement ot XMLAttrbute, which we want to set namespace as prefix for their name



public class Order
    {
        [XmlElement(ElementName = "OrderNum", Namespace= "http://sample.com")]
        public int OrderNumber { get; set; }

        [XmlElement(ElementName = "ItemDetails")]
        public string Item { get; set; }

        [XmlElement(ElementName = "UserInfor")]
        public string User { get; set; }

        [XmlAttribute(AttributeName = "OrderVersion")]
        public string version { get; set; }

        [XmlNamespaceDeclarations]
        public XmlSerializerNamespaces xmlns = new XmlSerializerNamespaces();

        public Order()
        {
            xmlns.Add("sample", "http://sample.com");        
        }     
    }


XML String:

XML Serialization XMLElement Name with namespace

Now you can see in above XML string, it has <sample:OrderNum>12</sample:OrderNum>  xml element tag with namespace as prefix in place of <OrderNum>12</OrderNum>

Same thing we can apply for XMLAttribute xml tag also, we can set attribute name with namespace as prefix

Here is an example for XMLAttribute tag, having namespace as prefix for attribute name


public class Order
    {
        [XmlElement(ElementName = "OrderNum", Namespace= "http://sample.com")]
        public int OrderNumber { get; set; }

        [XmlElement(ElementName = "ItemDetails")]
        public string Item { get; set; }

        [XmlElement(ElementName = "UserInfor")]
        public string User { get; set; }

        [XmlAttribute(AttributeName = "OrderVersion", Namespace = "http://sample.com")]
        public string version { get; set; }

        [XmlNamespaceDeclarations]
        public XmlSerializerNamespaces xmlns = new XmlSerializerNamespaces();

        public Order()
        {
            xmlns.Add("sample", "http://sample.com");        
        }     
    }


XML String:

XML Serialization XMLAttribute Name with namespace

Now you can see in above XML string, it has sample:OrderVersion xml attribute tag with namespace as prefix  in place of OrderVersion


Other related posts:

Friday, October 11, 2019

XML Serialization: How to serialize a property as XML attribute in serialization

.NET framework provides a set of attributes class (for example, XMLElement, XMLAttribute etcs) which controls the XML serialization from .net object to XML.
 XMLAttribute is used to include a property of object as attribute in XML and you can also provide own the attribute name, which is different from property name.

Here is an example in C# to serialize a property as XML attribute.

   public class Order
    {
        public int OrderNumber { get; set; }
        public string Item { get; set; }
        public string User { get; set; }

        [XmlAttribute(AttributeName="OrderVersion")]
        public string version { get; set; }
    }

In above Order Class, version property is being decorated with XMLAttribute, this field will be serialized as XMLAttrbute “OrderVersion” of parent XML Element “Order”

Order order = new Order { Item = "Iphone 7",  OrderNumber = 12, User = "Smith", version = "1.00" };

Here is serialized XML string corresponding from  above Order instance


XML Serialization - XMLAttribute


Other related posts:

XML Serialization: How to serialize an object to xml in C#


Serialization is the process of converting an object into a format (for example stream of bytes) that can be stored (for example in database, in a file) or transmitted over network. The reverse process is called De-serialization.

How to serialize an object to xml in C#

There are various data Serialization formats to convert object into serializable formats (for example bytes, XML, JSON)


 1.   XML serialization:
XML serialization serializes the public fields and properties of an object, or the parameters and return values of methods, into an XML stream that conforms to a specific XML Schema definition language (XSD) document

2.   Binary serialization :
Binary serialization uses binary encoding to produce compact serialization for uses such as storage or socket-based network streams.

3.   JSON serialization :
JSON serialization is used to serialize objects into JSON-encoded data, which is very efficient to transfer the small amount of data between server and client browser 


In this blog, we will discuss about the XML Serialization and how to convert C # object into XML format.

Microsoft provide the XmlSerializer class to serialize the .net Type into XML format and XmlTextWriter to output the XML string.

Here is a C# example of XML serialization

  public class Serializer
    {
        public string ToXML<T>(T Object)
        {
            StringWriter  stringWriter = new StringWriter();
            XmlTextWriter xmlTextWriter = null;

            XmlSerializer serializer = new XmlSerializer(Object.GetType());
            xmlTextWriter = new XmlTextWriter(stringWriter);
            serializer.Serialize(xmlTextWriter, Object);

            return stringWriter.ToString();
        }
    }


Now let’s create a .net object ‘Order’ for XML serialization   

   public class Order
    {
        public int OrderNumber { get; set; }
        public string Item { get; set; }
        public string User { get; set; }
    }

Create an instance of Order Class and try to convert order object into XML

     Order order = new Order {
Item = "Iphone 7",
 OrderNumber = 12,
 User = "Smith"
};

     Serializer serializer = new Serializer();
     string xml = serializer.ToXML<Order>(order);

When run the above code, you will get below XML string from Order instance.



Other related posts:

Tuesday, October 8, 2019

What are the benefits of unit testing?

Unit testing is a software testing mechanism to determine a unit of code is working correctly and also it enforces to segregate the programming code into a smaller unit and a small unit will be always easy for developer to design, code and test compare to big module.


unit testing life cycle
Unit Testing Life Cycle


There is always a discussion happened in team, why we should spend time to write the unit testing and what’s benefit of having it.

In this article, on basis of my many years’ experience in writing unit test, I will try to explain what’s benefit you will be have to introducing the unit testing into development process.

1.   Better Design:
Before writing of actual code, we should write the unit test, it enforce you to think about the better design and deeply understanding of responsibility of component, which you are going to develop.

2.   Quality of Code
Unit testing improves the quality of the code.  In early phase, it identifies every defect that may have come up before code is sent further for deployment. Unit testing also enforce you to write the more decouple and reusable of code.

3.   Enforce Agile Process:
    It is very risky to change of old design code. In agile process, we should always ready for new requirement or changes in existing requirement and changing in existing and testable design code is very risky but if unit test is proper placed it gives confidence to developer to modify existing design or re-introduce new design.

4.   Reduce of Cost:
if you think in long term of project, unit testing always reduce the project cost, if unit test is written for developed software, the maintenance becomes more easy for developer and he can confidently modified existing code and quickly test modified code with help of unit testing and also for new development, the bugs are found early, unit testing helps reduce the cost of bug fixes. Imagine the cost of a bug found during the later stages of development, like during system testing or during acceptance testing. Of course, bugs detected earlier are easier to fix because bugs detected later are usually the result of many changes, and you don’t really know which one caused the bug.

Monday, October 7, 2019

SSIS Exception - The Script Task uses version 15.0 script that is not supported in this release of Integration Services

I recently encountered SSIS exception - The Script Task uses version 15.0 script that is not supported in this release of Integration Services , when I included the script task in SSIS package to zip the generated text file and tried to run the SSIS package through command prompt.

   Source: Script Task Script Task
   Description: There was an exception while loading Script Task from XML: System.Exception: The Script Task "ST_08f0882909d74636bd9612a8d0e85790" uses version 15.0 script that is not supported in this release of Integration Services. To run the package, use the Script Task to create a new VSTA script. In most cases, scripts are converted  automatically to use a supported version, when you open a SQL Server Integration Services package in %SQL_PRODUCT_SHORT_NAME% Integration Services.
   at Microsoft.SqlServer.Dts.Tasks.ScriptTask.ScriptTask.LoadFromXML(XmlElement elemProj, IDTSInfoEvents events)
End Error

SSIS Package:

SSIS Package - Script Task

Here is command script to run ETL SSIS package

C:\Users\rtiwari>DTEXEC /FILE C:\Users\rtiwari\source\repos\PDFSharp_Merge\ETL_Extract.dtsx


After researching all possible cause, I found there could be two reason, why this SSIS exception is getting encountered.

1.   Deployed SSIS package ‘s target framework is not matching on deployed server :

Here is steps to fix the target framework for deployed SSIS package

Ø  Right click the SSIS project in the Project Explorer -> Click on Property


Ø  Click on Configuration Properties and Change TargetServerVersion è SQL Server 2017 



2.   Second reason really surprised me it was version of DTEXEC.exe. If the version of DTEXEC.exe command is not latest version, then also it throw exception.

Might be there are different version of SQL server Data Tool installed on your machine, Please look the below folders for different version of DTEXEC.exe
               
·         Open a Command Prompt window, change to the directory that contains the latest version of utility command, and then run the utility from that location
C:\Program Files(x86)\Microsoft SQL Server\140\DTS\Binn> DTEXEC /FILE C:\Users\rtiwari\source\repos\PDFSharp_Merge\ETL_Extract.dtsx
·         At the command prompt, run the utility DTEXEC.exe by entering the full path (:\Program Files\Microsoft SQL Server\140\DTS\Binn).
C:\Program Files(x86)\Microsoft SQL Server\140\DTS\Binn\DTEXEC.exe /FILE C:\Users\rtiwari\source\repos\PDFSharp_Merge\ETL_Extract.dtsx



SQL Server - Identify unused indexes

 In this blog, we learn about the index usage information (SYS.DM_DB_INDEX_USAGE_STATS) and analyze the index usage data (USER_SEEKS, USER_S...