Home > .NET Development > How To: Convert Bussiness object to XML string

How To: Convert Bussiness object to XML string

There are some case, where we need to update multiple records to database; and say those records are in Collection of object.
The simple solution for this is – Convert collection object into XML and send that XML string as parameter to Stored procedure.
In this way we can Insert/Update multiple records in a single database call.
 
Below is the function to convert an Object to XML string.
 
Public Function ObjectToString(Of T)(ByVal msg As T) As String
    Dim _dcs As New System.Runtime.Serialization.DataContractSerializer(GetType(T))
    Dim _sb As New System.Text.StringBuilder
    Dim _xmlwriter As Xml.XmlWriter = Xml.XmlWriter.Create(_sb)
    Dim _myString As String
    Try
        _dcs.WriteObject(_xmlwriter, msg)
        _xmlwriter.Flush()
        _myString = _sb.ToString
    Catch ex As Exception
        _myString = ex.ToString
    Finally
        _xmlwriter.Close()
    End Try
    Return _myString
End Function
 
Happy programming..!Smile
Advertisements
Categories: .NET Development
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: