Congratulations to all monthly winners of May 2013 !!! They have won INR 2900 cash and INR 27497 worth prize.
Go to DotNetFunda.com
Twitter TwitterLinkedIn
YouTubeGoogle
 Online : 14544 |  Welcome, Guest!   Register  Login
Home > Articles > ASP.NET > Various options to convert Business Entity Class as XML

Various options to convert Business Entity Class as XML

Article posted by Bibhas.paul on 2/13/2010 | Views: 7272 | Category: ASP.NET | Level: Beginner red flag

Advertisements

Advertisements
Various options to convert Business Entity Class as XML

Download


 Download source code for Various options to convert Business Entity Class as XML


Introduction
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.

Advantage

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(); // Sets user property
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(); // Sets user property
string strUserXML = Serialize.ToXML(objUserBO);

Both in the two case Reflection and Serialization will depict following XML.

Conclusion

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)

   // Copy above code…..
}

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>();
lstUsers.Add(PopulateUser());
lstUsers.Add(PopulateUser());

string strUserXML = Serialize.ToXML(lstUsers);

In the other hand Reflection require lot of alteration in the code to achieve this.

Advertisements

If you like this article, subscribe to our RSS Feed. You can also subscribe via email to our Interview Questions, Codes and Forums section.

Page copy protected against web site content infringement by Copyscape
Found interesting? Add this to:



Please Sign In to vote for this post.

About Bibhas Paul

Experience:8 year(s)
Home page:
Member since:Saturday, September 26, 2009
Level:Starter
Status: [Member]
Biography:Bibhas has 8 years of extensive experience in application development with exposure to business requirement study, system analysis and designing, coding ,testing,
implementation, end user training and client Interaction.

He was the part of application development team and worked for industry leading organizations like "ConocoPhillips", "Abbey National Bank" and "DHL".

Posses sound experience as a technical architect and all part of software development lifecycle.

His interest includes in Microsoft Technologies (ASP.NET 3.5 & SQL Server), Design Pattern and wide variety of internet technologies like AJAX, JQUERY etc.
 Responses
Posted by: Abhi2434 | Posted on: 14 Feb 2010 09:59:19 AM

Object Serializer does the same thing that you used in your first option.

If you serialize an object to XML, the serializer does takes all the type elements and values to create the xml file.

So you just implemented the Serialization of an object to XML.

And I dont believe, why you didnt take the help of XElement. You can create XElement easily, making sure that the xml you build is ok.

I hate building XML using StringBuilder.

Anyway ... Seems to be fine. I like your work

Cheers

Posted by: Bibhas.paul | Posted on: 20 Feb 2010 08:33:48 AM

Hi Abhi2434,
Thanks for your input. I have updated the part

>> Write Response - Respond to this post and get points
Related Posts

This article discusses about one of the new feature "Client ID generation" of ASP.NET 4.0

This article show glow on any message

Generally hosting .aspx page as services is not suggested as Web Service is especially built for this purpose. However, in some scenario this is likely to avoid an extra layer of SAOP because of Web Services protocol and to avoid adding reference to the consuming clients.

In this post I show you what are httpmodule in asp.net and how can you develop your custom url rewriter solution. A post worth reading.

In this article, we have consolidated all the GridView related articles covering many concepts which would serve as one stop reference.

More ...
About Us | Contact Us | The Team | Advertise | Software Development | Write for us | Testimonials | Privacy Policy | Terms of Use | Link Exchange | Members | Go Top
General Notice: If you find plagiarised (copied) contents on this page, please let us know the original source along with your correct email id (to communicate) for further action.
Copyright © DotNetFunda.Com. All Rights Reserved. Copying or mimicking the site design and layout is prohibited. Logos, company names used here if any are only for reference purposes and they may be respective owner's right or trademarks. | 6/19/2013 4:24:10 AM