Various options to convert Business Entity Class as XML

Bibhas.paul
Posted by in ASP.NET category on for Beginner level | Views : 10664 red flag

Various options to convert Business Entity Class as XML


 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.

Page copy protected against web site content infringement by Copyscape

About the Author

Bibhas.paul
Full Name: Bibhas Paul
Member Level:
Member Status: Member
Member Since: 9/26/2009 12:53:40 AM
Country: India
BibhasPaul

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.

Login to vote for this post.

Comments or Responses

Posted by: Abhi2434 on: 2/14/2010
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 on: 2/20/2010
Hi Abhi2434,
Thanks for your input. I have updated the part

Login to post response

Comment using Facebook(Author doesn't get notification)