Article posted by Bibhas.paul
on 2/13/2010 | Views: 7272 | Category: ASP.NET
| Level: Beginner
Various options to convert Business Entity Class as XML
Download Download source code for Various options to convert Business Entity Class as XML
Business entity class is commonly known as custom entity or data container or data transfer object (DTO). Purpose of business entity class is to carry data across the application. In this article we will discuss about how to translate business entity into XML.
To maintain simplicity in the Database part, I always write store procedure in such fashion so that it should accept only single input parameter. Passing XML as a parameter to backend is the easiest way to achieve the same. What I do generally, in the application layer I convert my business entity class into XML string and send it across to database. In the database I parse the XML to retrieve the data and work accordingly. It eventually brings down application complexity and accelerates the performance. This approach specially helps me when I deal with entity class having numerous public properties or I need to pass such entity as a collection to database. Creating XML from entity or entity collection and send it to the database would be the easier way.
Various way to translate Entity Class as XML
We will discuss two mechanisms to interpret Entity class as XML. One is Reflection and another is Serialization. I am not going argue which option is the best rather I will try to discuss the mechanism.
Custom Entity Class
Assume we have a custom entity class named UserBO with Name, Phone and Email as public properties. We will refer this entity throughout the discussion.
Whenever require we will create the instance of UserBo and will set its property. See below the example
XML conversion through Reflection
In this process I have constructed the XML string manually. Logic is to retrieve object properties and loop through it. In the below mentioned code I have created a method ToXML which accepts a business entity and in turn it returns the corresponding XML as string. Add a static class Reflection and write following code.
Write following code to invoke above function
UserBO objUserBO = PopulateUser(); string strUserXML = Reflection.ToXML(objUserBO);
XML conversion through Serialization
This is rather easy way compare to reflection. We will consider XML serialization to construct XML from an entity. See below the code
Write following code to invoke this function
UserBO objUserBO = PopulateUser(); string strUserXML = Serialize.ToXML(objUserBO);
Both in the two case Reflection and Serialization will depict following XML.
Among the two mechanisms that we have already discussed, XML Serialization is the easiest and popular way because of its usefulness. With little modification to Serialize.ToXML(….) can convert any object to XML. See below the way.
public static string ToXML(Object DTO)
This approach is helpful when we plan to use entity collection as a replacement of ADO.NET data table and would like to generate XML of such entity collection.
List<UserBO> lstUsers = new List<UserBO>();
string strUserXML = Serialize.ToXML(lstUsers);
In the other hand Reflection require lot of alteration in the code to achieve this.
If you like this article, subscribe to our RSS Feed. You can also subscribe via email to our Interview Questions, Codes and Forums section.
Found interesting? Add this to: