BizTalk Server 2004 : B2B, EAI and .NET

Monday, September 27, 2004

Using XmlSerializer in the BizTalk Web Service Proxy.

This is something that most people will be aware of but might be helpful for newbies. The BizTalk Web Services Publishing Wizard, creates new datatypes for input and output messages during creation of the Web Service that exposes Orch/Schemas. Adding a web reference to this web service will generate the same datatypes, whose definitions can be found in the Reference.cs file. If the external system already generates XML, creating the input message object can be a drag.
A quick way of creating the input message objects from XML and converting the output message objects to XML, is by using the XML Serializer class. Here's the sample code.

//creating a new RequestMsg
objectRequestMsg objRequestMsg = new RequestMsg();

//Create an XML Serializer for RequestMsg document
XmlSerializer serRequestMsg = new XmlSerializer(objRequestMsg.GetType(), "http://schemas.devdutt.com/XMLSchema/v1/InputMsg.xsd");

//Deserializing XML into RequestMsg class
//strRequestMsgXml is the XML String
objRequestMsg = (RequestMsg)(serRequestMsg.Deserialize(new XmlTextReader(new StringReader(strRequestMsgXml))));

//Call RequestMsg Web Service and send the Request
//orchInvoker is the Web Service Proxy Object
//Submit is the Web Method
object obj = (orchInvoker.Submit(objRequestMsg));

//Create an XML Serializer for OutputMsg document
XmlSerializer serResponseMsg = new XmlSerializer(obj.GetType(), "http://schemas.devdutt.com/XMLSchema/v1/OutputMsg.xsd");

//Create a temporary memory object for storing xml stream
MemoryStream ms = new MemoryStream();

//Serialize the output document
serResponseMsg.Serialize(ms, obj);

//Convert the memory stream into the string buffer
string strResponseMsgXml = System.Text.Encoding.ASCII.GetString(ms.GetBuffer());

Any questions, post a comment.


0 Comments:

Post a Comment