Archive

Archive for November, 2008

How To: Convert Bussiness object to XML string

November 17, 2008 Leave a comment
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