.NET Framework Developer Center >
.NET Development Forums
>
ASMX Web Services and XML Serialization
>
Exposing Business Objects
Exposing Business Objects
- I want to pass a BO that is a collection of different other BO's into my WS, i am using C# to create my WS how do i expose my BO's to Clients?
Answers
- You need to serialize the BO objects to String and then u can get the string in Windows forms or ASP.NET web forms by de-serializing back to object...
Code to Serilize
------------------
public string XMLSerialize(object pObject)
{
try
{
XmlSerializer objXMLSerilize = new XmlSerializer(pObject.GetType());
using (StringWriter stream = new StringWriter())
{
objXMLSerilize.Serialize(stream, pObject); stream.Flush();
return stream.ToString();
}
}
catch (InvalidOperationException ex)
{
throw ex;
}
}
Code to De-Serialize
------------------------
public object XMLDeSerialize(object pObject, string strXMLString)
{
try
{
XmlSerializer objXMLSerilize = new XmlSerializer(pObject.GetType());
using (StringReader stream = new StringReader(strXMLString))
{
return objXMLSerilize.Deserialize(stream);
}
}
catch (InvalidOperationException ex)
{
throw new InvalidOperationException("Failed to create object from xml string", ex);
}
}
How to Serialize using the above code
--------------------------------------------
fg_sXMLString = XMLSerialize(objFOTrnCodeMstList);
How to De-Serialize using the above code
--------------------------------------------
objFOTrnCodeMstList = (List<FOTrnCodeMst>)XMLDeSerialize(objFOTrnCodeMstList, fg_sXMLString);
in the above code objFOTrnCodeMstList is my BO object, and fg_sXMLString is the serialized string
sounds simple.... just try it out :-)
Narayanan Dayalan - Zeetaa Business Solutions- Marked As Answer byAmadeo Casas - MSFTModeratorMonday, November 02, 2009 8:45 PM
All Replies
- Hi,You will have to convert the custom collection to a list or array so that they can be serialized. Look at this exampleIt uses generics to return a list of custom Business Objects.
- You need to serialize the BO objects to String and then u can get the string in Windows forms or ASP.NET web forms by de-serializing back to object...
Code to Serilize
------------------
public string XMLSerialize(object pObject)
{
try
{
XmlSerializer objXMLSerilize = new XmlSerializer(pObject.GetType());
using (StringWriter stream = new StringWriter())
{
objXMLSerilize.Serialize(stream, pObject); stream.Flush();
return stream.ToString();
}
}
catch (InvalidOperationException ex)
{
throw ex;
}
}
Code to De-Serialize
------------------------
public object XMLDeSerialize(object pObject, string strXMLString)
{
try
{
XmlSerializer objXMLSerilize = new XmlSerializer(pObject.GetType());
using (StringReader stream = new StringReader(strXMLString))
{
return objXMLSerilize.Deserialize(stream);
}
}
catch (InvalidOperationException ex)
{
throw new InvalidOperationException("Failed to create object from xml string", ex);
}
}
How to Serialize using the above code
--------------------------------------------
fg_sXMLString = XMLSerialize(objFOTrnCodeMstList);
How to De-Serialize using the above code
--------------------------------------------
objFOTrnCodeMstList = (List<FOTrnCodeMst>)XMLDeSerialize(objFOTrnCodeMstList, fg_sXMLString);
in the above code objFOTrnCodeMstList is my BO object, and fg_sXMLString is the serialized string
sounds simple.... just try it out :-)
Narayanan Dayalan - Zeetaa Business Solutions- Marked As Answer byAmadeo Casas - MSFTModeratorMonday, November 02, 2009 8:45 PM


